Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Moe   Crossbones+   -  Reputation: 1249

Like
0Likes
Like

Posted 25 June 2008 - 05:21 AM

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?

Sponsor:

#2 Evil Steve   Members   -  Reputation: 1987

Like
0Likes
Like

Posted 25 June 2008 - 05:27 AM

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.

#3 Moe   Crossbones+   -  Reputation: 1249

Like
0Likes
Like

Posted 25 June 2008 - 05:35 AM

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.


#4 TheTroll   Members   -  Reputation: 882

Like
0Likes
Like

Posted 25 June 2008 - 05:55 AM

The TopMost property set to true will work for you but only if you use .Show(), not .ShowDialog().

theTroll

#5 Moe   Crossbones+   -  Reputation: 1249

Like
0Likes
Like

Posted 25 June 2008 - 05:58 AM

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.



#6 Headkaze   Members   -  Reputation: 583

Like
0Likes
Like

Posted 25 June 2008 - 06:36 AM

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();


#7 TheTroll   Members   -  Reputation: 882

Like
0Likes
Like

Posted 25 June 2008 - 07:54 AM

yourForm.TopMost = true;
yourForm.Show();
yourForm.Focus();

Tada.

theTroll

#8 Moe   Crossbones+   -  Reputation: 1249

Like
0Likes
Like

Posted 25 June 2008 - 08:02 AM

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.

#9 thomse31   Members   -  Reputation: 122

Like
0Likes
Like

Posted 22 September 2008 - 06:41 AM

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



#10 Cryogenic   Members   -  Reputation: 136

Like
0Likes
Like

Posted 23 September 2008 - 02:37 AM

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);
}

#11 GutsFun   Members   -  Reputation: 100

Like
0Likes
Like

Posted 15 July 2009 - 02:41 AM

Thanks a lot Cryogenic for this response !! It help me a lot !

For those who search the equivalent in VB.NET here it is :

Declare Auto Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Integer

Private Sub OnShown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
SetForegroundWindow(Me.Handle)
End Sub




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS