[.net] ShowDialog window hiding behind other windows...

Started by
9 comments, last by GutsFun 14 years, 9 months ago
So we've stumbled across something strange at work. We've got a login screen to our application, which is displayed using a ShowDialog() call. For some odd reason, this window sometimes appears behind other windows, which occasionally confuses the users. We've tried using a combination of ShowDialog(), SendToBack(), BringToFront(), and Activate(), and messing with the properties to make it the topmost window, none of which have worked. Previous to the login dialog showing, we have a splash screen that spawns a separate thread and does some things in that thread. My co-worker thinks that the spawned thread is getting the focus, and in doing so our login dialog isn't popping to the front as it should. I have done a bit of googling on the subject, and the most common solution seems to be to call SendToBack() then BringToFront(). Unfortunately, this doesn't seem to work. It works fine when launching the app from the IDE, but not when running it from windows explorer. So, anyone have any thoughts?
Advertisement
You're not using GetActiveWindow(), GetForegroundWindow() or any such function to get the parent window for the new dialog are you? (Or whatever the .NET equivalents are). That could certainly cause this.
Quote:Original post by Evil Steve
You're not using GetActiveWindow(), GetForegroundWindow() or any such function to get the parent window for the new dialog are you? (Or whatever the .NET equivalents are). That could certainly cause this.

I don't believe we are. I just did a quick search for both GetActiveWindow and GetForegroundWindow, and didn't find either being called anywhere in the app.
The TopMost property set to true will work for you but only if you use .Show(), not .ShowDialog().

theTroll
Quote:Original post by TheTroll
The TopMost property set to true will work for you but only if you use .Show(), not .ShowDialog().

Apparently this does bring it to the front (as it should), but doesn't give it focus.

Isn't ShowDialog() modal, so designed to display a form with a parent? Ie ShowDialog(this) would show the modal dialog with "this" as the parent. Perhaps you could get a handle to topmost window using GetForegroundWindow() then use that as the firt parameter to ShowDialog()? Never tried that myself though.

Or as TheTroll said use Show() (which is non-modal) instead with the TopMost property (you can actually set it to true then false just to bring it to top of z order).

To get focus use this.Focus() then this.Activate().

So in other words try
this.TopMost = true;this.TopMost = false;this.Focus();this.Activate();
yourForm.TopMost = true;
yourForm.Show();
yourForm.Focus();

Tada.

theTroll
I just chatted with my co-worker, and he said he had tried .Focus(), which didn't work. He was able to get it working by messing around with some of the threading stuff that we had in the startup portion of the program. Now, rather than the splash screen starting a thread that does stuff, it all just does it from the main thread.

Thanks for the help guys.
Hi,

I have been struggling for some time with the same problem as described above, and finally found a solution for this.

When testing my app, I saw that if I gave a mouse click on the splash screen, the Login form would afterwards have the correct focus. The solution is simply to make a codewise mouseclick on the splash (NOT the Login). Put the following in the splash (I also have the Me.Active in the Login Load, but I have not tested if it is necessary):

Declare Function mouse_event Lib "user32.dll" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long) As Long

Const MOUSEEVENT_LeftDown As UInt32 = &H2
Const MOUSEEVENT_LeftUp As UInt32 = &H4


Private Sub Splash_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

....
....
....

Me.Activate()
BringFocusByMouseClickHack()

End Sub

Private Sub BringFocusByMouseClickHack()
Try
mouse_event(MOUSEEVENT_LeftDown, 0, 0, 0, 0)
mouse_event(MOUSEEVENT_LeftUp, 0, 0, 0, 0)

Catch
End Try
End Sub

use SetForegroundWindow http://msdn.microsoft.com/en-us/library/ms633539(VS.85).aspx inside the form you want to receive focus

[DllImport("user32")]
public static extern int SetForegroundWindow(IntPtr hwnd);

and when the dialog form is shown call the function with the form handle;

void OnShown(object sender, EventArgs e) {
SetForegroundWindow(this.Handle);
}

This topic is closed to new replies.

Advertisement