Problem: Internet Explorer window steals focus from active .NET WinForms application. The open IE window acts like it gets adopted into the WinForm application so when the child window is closed from the parent Winform instead of focus going back the parent window it goes to the IE web page. However, the IE 7 window is NOT active but in the foreground. Starting IE 7 web page and starting the .NET Winform application this does NOT occur.
Test platform: Windows XP Pro SP3/ IE 7 and also Windows 7/ IE 8. VS 2008 WinForm application (VB). .NET framework 2.0
Following the steps below to duplicate but does not always happen –
Create a WinForm application with at least one child form (not MDI). The main form should be loaded at startup.
Now startup IE 7 to any active web page. Leave page display on active desktop
From the main WinForm window open the child window doing some processing (i.e. data retrieval, etc.).
Close the child window which should return focus to the main Winform in the front of the desktop window stack. What happens is that IE window inserts in front of the main form and the child form. You can also add message box prompt here which causes IE to insert between the message box and form it was called from.
Sample code:
I have included the optional parent “me or this” (vb /C#) when opening a modal window.
Button Click event
ChildForm = new ChildForm
ChildForm.ShowDialog(me)
Do stuff in the child form -display data in list or something and do some background process.
[Child form - close or hide)
ChildForm.Close
ChildForm.Dispose
End
Instead of focus returning to the parent form IE window is on top on the desktop while the parent form retains focus but is not the top most window.
This does not happen every time consistently so repeated attempts need to be necessary to duplicate.
If IE window is minimized it does not happen. If IE is started first before the .NET Winform application is does not happen. At one point IE window acted like a child modal window of the .NET Winform application. I couldn’t click the title bar to set focus to the application.
I was able to manually control focus by using "BringToFront" but does not work with message box.
Ray Whittemore - Senior software engineer