16

I'm writing a Windows Forms Application in C#.NET

On startup, the application displays a splash screen which is running in a separate thread. Whilst the splash screen is showing, the main application is initialising.

Once the main application has finished initialising, the main form of the application is displayed, and the splash screen still shows over the top.

Everything so far is as expected.

Then, the Splash screen is closed, which causes that thread to exit. For some reason, at the point, the main application windows gets sent behind all other open Windows, notably the Windows Explorer window where you clicked the .exe file to run the application in the first place!

What could be causing the windows to suddenly jump "behind" like this?

4 Answers 4

11

Try calling .Activate() on your main window when your thread closes.

It's never been active, and thus has low Z-Order, so whatever is higher will naturally be above it. I had to fix this exact scenario in our app.

Don't forget! You may need to marshal the call to the correct thread using an Invoke()!

1

I've had this happen at times too. Bob's response is the easiest and works for me in the majority of cases. However, there have been some times where I need to use brute force. Do this via interop like this:

[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);

0

Is the splash screen a Modal dialog?

I have seen this window 'jumping' if you dismiss a Modal dialog twice by setting both DialogResult and calling Hide() or close().

Code like this:

private void button1_Click(object sender, System.EventArgs e)
{
     this.DialogResult = DialogResult.Abort;
     this.Hide();
}

See this blog entry for all of the cases...

-3

You can use code like this :

private void button1_Click(object sender, System.EventArgs e)
{
     this.DialogResult = DialogResult.Abort;
     this.Hide();
}

Thanks all!

2
  • 1
    Repeating somebody's code snippet over a year after they have posted is not helpful to a reader. Jul 14, 2015 at 7:55
  • 1
    Especially when the code copied/pasting is an example of code that causes the problem.
    – damian
    Jan 25, 2016 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.