locked
Open Right Hand Pane and Set Focus RRS feed

  • Question

  • Hi All,

    I have created a Ribbon for Microsoft Word using Dotnet 4(C#).

    On click of my Ribbon item, the Right hand pane(RHP) is opened and the List of items are displayed in ItemControl with vertical Scroolbar which is present in RHP.
    However the focus is not on the RHP meaning that you can’t scroll with the mouse wheel. The mouse wheel still scrolls the Word doc which feels odd.

    I tried with the below code but its not working,

    private static void AddTaskPaneToActiveWindow(UserControl uc, string Title = null)
            {
                //add task pane if needed
                if (ThisAddIn.Application.Windows.Count > 0)
                //if at least 1 doc window is open
                {
                    var pane = ThisAddIn.CustomTaskPanes.Add(uc, Title ?? uc.GetType().Name, ThisAddIn.Application.ActiveDocument.ActiveWindow);
                    pane.Width = ActiveWindow.Width / 2;
                    pane.Visible = true;
                    pane.Control.Focus();
                }
                else
                {
                    var pane = ThisAddIn.CustomTaskPanes.Add(uc, Title ?? uc.GetType().Name);
                    pane.Width = 400;
                    pane.Visible = true;
                    pane.Control.Focus();
                }
            }

    Can someone please let me know how to set focus of RHP on displaying it.


    Regards,
    Chetan.


    Chetan Rajakumar

    • Moved by Steve Fan Thursday, January 15, 2015 6:11 AM coding related
    Wednesday, January 14, 2015 1:19 PM

Answers

  • Hi Fei,

    Below workaround i got from 'Word for Developer' forum works like charm.

    [DllImport("user32.dll")]
    static extern IntPtr SetFocus(IntPtr hWnd);

    private static void AddTaskPaneToActiveWindow(UserControl uc, string Title = null)
            {
                //add task pane if needed
                if (ThisAddIn.Application.Windows.Count > 0)
                //if at least 1 doc window is open
                {
                    var pane = ThisAddIn.CustomTaskPanes.Add(uc, Title ?? uc.GetType().Name, ThisAddIn.Application.ActiveDocument.ActiveWindow);
                    pane.Width = ActiveWindow.Width / 2;
                    pane.Visible = true;
                    SetFocus(pane.Control.Handle);
                }
                else
                {
                    var pane = ThisAddIn.CustomTaskPanes.Add(uc, Title ?? uc.GetType().Name);
                    pane.Width = 400;
                    pane.Visible = true;
                    SetFocus(pane.Control.Handle);
                }
            }

    Regards,


    Chetan Rajakumar

    Tuesday, January 20, 2015 9:15 AM

All replies

  • Hi,

    Since this issue is more related to coding, I'll move it to the following forum:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsto

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Steve Fan

    Forum Support


    Come back and mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback on our support, please click here

    Thursday, January 15, 2015 6:10 AM
  • Hi Chetn,

    According to the description, you couldn't set the focus when you show the custom task pane.

    I am trying to reproduce the issue in Word 2013 howeve failed. Here is code that associate with button on the ribbon:

      private void SetFocus_Click(object sender, RibbonControlEventArgs e)
            {
                CustomTaskPane ctp=Globals.ThisAddIn.CustomTaskPanes.Add(new UserControl1(), "MyCustomTaskPane");
                ctp.Visible=true;
                ctp.Width = Globals.ThisAddIn.Application.ActiveWindow.Width / 2;
                ctp.Control.Focus();
            }

    The focus was set successfully on the first table index control on the UserControl1. And the mouse wheel can't scrolls the Word document.

    What's version of Word are you using? Did I miss any step?

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 15, 2015 11:59 AM
  • Hi Fei,

    I tried with your code but still the focus is not set to CTP instead the mouse wheel scrolls the Word Document. I am using Word version 2010.

     private static void AddTaskPaneToActiveWindow(UserControl uc, string Title = null)
            {
                //add task pane if needed
                if (ThisAddIn.Application.Windows.Count > 0)
                //if at least 1 doc window is open
                {

                    //CustomTaskPane pane = Globals.ThisAddIn.CustomTaskPanes.Add(uc, Title ?? uc.GetType().Name, ThisAddIn.Application.ActiveDocument.ActiveWindow);
                    CustomTaskPane pane = Globals.ThisAddIn.CustomTaskPanes.Add(uc, "MyCustomTaskPane");
                    pane.Width = ActiveWindow.Width / 2;
                    pane.Visible = true;
                    pane.Control.Focus();
                }
                else
                {
                    //CustomTaskPane pane = Globals.ThisAddIn.CustomTaskPanes.Add(uc, Title ?? uc.GetType().Name);
                    CustomTaskPane pane = Globals.ThisAddIn.CustomTaskPanes.Add(uc, "MyCustomTaskPane");
                    pane.Width = 400;
                    pane.Visible = true;
                    pane.Control.Focus();
                }
            }

    Please let me know what can be done for this.

    Regards,


    Chetan Rajakumar

    Friday, January 16, 2015 4:31 AM
  • Hi Chetan,

    Thanks for the detail exploitation for this issue.

    I could reproduce this issue in Word 2010 too. It seem the issue is relative to the specific version of Word.

    Event I try to set the focus on the user control like code below, it still failed:

    ctp.Control.Controls["textBox1"].Text = "Hello";
    ctp.Control.Controls["textBox1"].Focus();

    Since the issue is complex, I suggest you contacting Microsoft support to raise an incident so that our engineer could work closely with him to identify the root cause and resolve this issue as soon as possible.

    If the support engineer determines that the issue is the result of a bug the service request will be a no-charge case and you won't be charged.

    Please visit the below link to see the various paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 19, 2015 8:47 AM
  • Thanks Fei,

    This issue is with Word 2010 and below, i have verified it working fine with Word 2013. As you said let me contact the microsoft support team for this issue and lets see if i get this issue solved asap.

    Regards,


    Chetan Rajakumar

    Monday, January 19, 2015 11:47 AM
  • Hi Fei,

    Below workaround i got from 'Word for Developer' forum works like charm.

    [DllImport("user32.dll")]
    static extern IntPtr SetFocus(IntPtr hWnd);

    private static void AddTaskPaneToActiveWindow(UserControl uc, string Title = null)
            {
                //add task pane if needed
                if (ThisAddIn.Application.Windows.Count > 0)
                //if at least 1 doc window is open
                {
                    var pane = ThisAddIn.CustomTaskPanes.Add(uc, Title ?? uc.GetType().Name, ThisAddIn.Application.ActiveDocument.ActiveWindow);
                    pane.Width = ActiveWindow.Width / 2;
                    pane.Visible = true;
                    SetFocus(pane.Control.Handle);
                }
                else
                {
                    var pane = ThisAddIn.CustomTaskPanes.Add(uc, Title ?? uc.GetType().Name);
                    pane.Width = 400;
                    pane.Visible = true;
                    SetFocus(pane.Control.Handle);
                }
            }

    Regards,


    Chetan Rajakumar

    Tuesday, January 20, 2015 9:15 AM
  • Hi Chetan,

    Thanks for sharing the workaround with us.

    It would be very helpful for others who have the same issue.

    Have a nice day:)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 21, 2015 2:54 AM