none
non-modal not retains focus when changing documents RRS feed

  • Question

  • Hi,

    I'm developing Word Add-In in C#. 

    I have a non-modal dialog that is opened from a particular document. If I have another document open, when i'm switching to it (while the dialog is still open on the other document) and going back to the original document, the dialog loses its focus.

    This not happens when switching between other programs, only on Word documents change. 

    How can I make it to retain the focus when changing back? I tried with 

    myDialog.BringToFront();


    and

    myDialog.Focus();

    But no luck.

    Is is something I did that caused to this behavior or this is how it normally works in VSTO (in VBA it works just fine).

    Thanks.

    Tuesday, March 25, 2014 11:50 AM

All replies

  • Hello,

    You need to specify a parent window handle for your dialog. The Show and ShowDialog methods of the System.Windows.Forms.Form class accepts an instance of the IWin32Window interface which is used for representing the Win32 HWND handles. You just need to create an instance of the following class:

    public class Win32Window : System.Windows.Forms.IWin32Window
    {
        private IntPtr _hwnd;
        public Win32Window(IntPtr handle)
        {
            _hwnd = handle;
        }
    
        public IntPtr Handle
        {
            get 
            {
               return _hwnd; 
            }
        }
    }

    The Creating a IWin32Window from a Win32 Handle  article describes the process of converting handles in depth.

    The Word object model provides the HWND property for the Window class. You can use it for specifying the parent window handle when you call the Show method of your form. 

    Tuesday, March 25, 2014 8:40 PM
  • I have this code to handle this:

    public partial class MyWin32Window : IWin32Window
            {
                private readonly IntPtr m_hwnd;
    
                public MyWin32Window(IntPtr hwnd)
                {
                    m_hwnd = hwnd;
                }
    
                public IntPtr Handle
                {
                    get
                    {
                        return m_hwnd;
                    }
                }
            }
    
    public static MyWin32Window getWordWindow()
    {
        IntPtr wordWindow = NativeMethods.FindWindowW("OpusApp", Globals.ThisAddIn.Application.ActiveWindow.Caption + " - Microsoft Word");
        MyWin32Window myWin = new MyWin32Window(wordWindow);
        return myWin;
    }

    And i'm sending getWordWindow() as the instance to my dialog Show(), ShowDialog().

    Wednesday, March 26, 2014 7:55 AM
  • Did it help? Do you see your form on top of the Word window now?
    Wednesday, March 26, 2014 8:21 AM
  • It's on top, but not activated. I have to click somewhere on it to get its focus first. This is what I'm trying to eliminate.

    Reminder: it's a non-modal dialog, so it can be on top and unactiavted.

    • Edited by etaiso Wednesday, March 26, 2014 9:09 AM
    Wednesday, March 26, 2014 9:08 AM
  • Yes, you are on the right avenue.

    If you want to see the dialog always on top and get focus automatically, you need to use the ShowDialog method instead of Show. It also allows you to specify the parent window handle specifying an instance of the IWin32Window interface.

    Wednesday, March 26, 2014 1:36 PM
  • Thanks for your help, but I can't use ShowDialog(), as I don't want it to block the document (similar to Find and Replace, CTRL+H, dialog in Word - as I know see suffers from the same issue/glitch).

    I can surely tell that a colleague of mine has successfully implemented a non-modal dialog using VBA which is not suffering from this issue.

    Wednesday, March 26, 2014 1:58 PM
  • Did your colleague try to implement a dialog in a .Net based add-in (or any COM add-in), not VBA?

    Wednesday, March 26, 2014 3:59 PM
  • No he didn't, this is where I come in :)

    I guess that if it's achievable in VBA, there must be some way to do that on .NET.

    Thursday, March 27, 2014 1:32 PM