46

Problem description

If I make a non-modal window as a child window through setting the Owner of the window to a parent window, and then show a MessageBox from within this child window, the parent window will lose focus if I close the child window. If windows explorer or another app is open, this app will get the focus and my main window will be hidden.

This seems to be a known problem as I saw it in another newsgroups, but I don’t have seen a good solution. Setting the owner to null in OnDeactivate is not an option. Setting the owner before showing the MessageBox to null and resetting after that doesn’t help. Setting the owner to null in the OnClosed event does also not help.

Simple Solution found

If you experience the same problem as I have described, put the following code in the OnClosing of all child windows.

void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    base.OnClosing(e);
    if (null != Owner) {
        Owner.Activate();
    }
    // ....
}

It can be followed by any further processing logic, even opening MessageBoxes is tolerated.

Example-Code

The issue seems to be much bigger as I thought. The following example will remove focus of the parent window if the message box will be opened and the the child window will be closed (Copy the code into a loaded event-handler of a Window).

Window firstChildWindow = new Window() {
    Title = "Floating Window", Width = 100, Height = 70
};
firstChildWindow.Owner = Window.GetWindow(this);    
Button button = new Button() { Content="MessageBox"};
button.Click += delegate { MessageBox.Show("Klicking her breaks the focus-chain."); };
firstChildWindow.Content = button;
firstChildWindow.Show();

Also this example breaks the focus-chain:

Window firstChildWindow = new Window() {
    Title = "Floating Window", Width = 100, Height = 70
};
firstChildWindow.Owner = Window.GetWindow(this);
firstChildWindow.Show();    
Window secondChildWindow = new Window() { Title="Second Window", Width=100, Height=70};
secondChildWindow.Content = new TextBlock() { Text="SecondWindow"};
secondChildWindow.Owner = firstChildWindow;
secondChildWindow.Show();

Has someone a resolution for this problem. I think about a hack to trigger giving focus to the parent after closing, with Dispatcher or DispachterTimer or perhaps it would be work to manually force focus to the parent on closed but this all seems to me very unclean (and is also a little complicated if there are more active owned windows of the same parent, as I have in my current app).

No one knows a neat solution to this?

Resources

MSDN Description (see under remarks for non modal windows opened calling Show())

Same problem on MSDN forums without appropriate solution

Please see also: Instable focus of WPF apps

Toni
  • 1,555
  • 4
  • 15
  • 23
HCL
  • 36,053
  • 27
  • 163
  • 213

8 Answers8

29

What most likely happened here is you have two independent top level windows and closed one of them. This will sometimes cause focus to jump to another application.

This also happens if one windows owns a second, which in turn owns a third. Probable BUG in the Win32 API but its been with us forever so good luck getting it fixed.

The workaround is to manually hand focus back to child in the Closing event of the grandchild. But in this case, grandchild is a messagebox so you can't do that. The easiest thing to do is to own messagebox to parent. The next easiest is to make your own messagebox control.

Noctis
  • 11,507
  • 3
  • 43
  • 82
Joshua
  • 40,822
  • 8
  • 72
  • 132
  • The second paragraph describes exactly my problem. Do you have more information on this? – HCL Jul 14 '10 at 17:28
  • No unfortunately I don't. I encountered the bug writing Win32 code a long time ago. The only solution was to process WM_CLOSE and call SetFocus() back to the window's owner. In WinForms, WM_CLOSE translates to ϞFormClosing, and there is a Focus() method on the Form class. There's got to be similar stuff for WPF. – Joshua Jul 14 '10 at 20:12
  • Copy/paste. It looks like the intellisense icon for event. – Joshua Jan 17 '11 at 21:26
26

This is a very annoying bug, the most common solution provided is:

call Me.Owner.Focus in the Window_Unloaded or Window_Closing event.

But that still doesnt work 100%, you still see the background-window flashing to the foreground (very briefly), before focus is restored correctly.

I found a method which works better:

call Me.Owner.Activate before Me.Close (or in the _Closing event).

Noctis
  • 11,507
  • 3
  • 43
  • 82
Maestro
  • 9,046
  • 15
  • 83
  • 116
  • Looks like I'll have to do this. I'm going to put it into a reusable behaviour though so all my views can use this for good measure. Just as a side note, seems like I get this issue with a scenario different to the two the OP mentions. – Andy Dec 14 '12 at 09:47
  • I also used Owner.Focus() in addition to Owner.Activate(); otherwise, my keyboard shortcuts weren't working right away. – scottheckel Jun 09 '15 at 18:28
4

2020 and still in trouble...

Workaround for Wpf :

    public ContactsWindow()
    {
        InitializeComponent();

        Closing += ContactsWindow_Closing;
    }

    private void ContactsWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        Owner?.Activate();
    }

It seems to work for me, interested in any news

PascalPr
  • 11
  • 2
2

Maybe i am quite late on the matter.
@HCL this problem could be resolved more easily by setting the options parameter of the MessageBox.show() to MessageBoxOptions.None.

Cheers...

Rizvi Hasan
  • 703
  • 1
  • 5
  • 19
0

Johsua's .Activate (see above) solution works quite nicely with all sorts of window types. At least it did the trick for me.

0

i had a similar problem. I had a one main window, where rest child windows was assign to that window via Owner property. If main window had two or more children, after close second child, app lose focus. I used trick with child.Owner = null before close, but then my next child window lose focus. I handled it by getting all children and set focus to the last one before closing child. Here is code example:

private void OnClosing(object sender, CancelEventArgs cancelEventArgs)
        {
            var childWindows = (sender as Window).Owner.OwnedWindows;
            if (childWindows.Count - 2 >= 0)
                childWindows[childWindows.Count - 2].Focus();
            else
            {
                (sender as Window).Owner.Focus();
            }
            (sender as Window).Owner = null;
        }
darthkurak
  • 157
  • 1
  • 2
  • 12
0

it is possible to write your own messagebox, you can implement it as a floater the disable entire screen when showed.

you check the caliburn mvvm frameork sample, there is a nice implementation of messagebox as service.

Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141
0

Ownership has nothing to do with which window activation. To see this, change the owner of the second window to the top window and run the program. Notice that nothing changes. Ownership has to do with "If a window is minimized what other windows need to be minimized". So you are stuck with using custom activation code.

jyoung
  • 5,071
  • 4
  • 30
  • 47