What is the "right" way to bring a Windows Forms Application to the foreground?
Solution 1
Have you tried Form.Activate?
This code seems to do what you want, by restoring the form to normal size if minimized and then activating it to set the focus:
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
this.Activate();
Warning: this is annoying! If it's just an app for your personal use, as you say, maybe you can live with it. :)
Solution 2
Note. Below I have copied the most voted answer in a linked question closed as a duplicate to this one. That answer is the only pure C# answer I've found that solves this problem.
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;
It always brings the desired window to the front of all the others.
Solution 3
private static class User32
{
[DllImport("User32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
internal static readonly IntPtr InvalidHandleValue = IntPtr.Zero;
internal const int SW_MAXIMIZE = 3;
}
public void Activate()
{
Process currentProcess = Process.GetCurrentProcess();
IntPtr hWnd = currentProcess.MainWindowHandle;
if (hWnd != User32.InvalidHandleValue)
{
User32.SetForegroundWindow(hWnd);
User32.ShowWindow(hWnd, User32.SW_MAXIMIZE);
}
}
Solution 4
You can set .TopMost
to true, call DoEvents()
, and then set .TopMost
back to false. It's still hackish, but if Activate and Show aren't working it's better than minimizing/re-showing.
Solution 5
After several attempts I found out working combination:
form.Show();
form.WindowState = FormWindowState.Normal;
form.Activate();
D'Arcy Rittich
Updated on April 23, 2020Comments
-
D'Arcy Rittich about 4 years
I am writing a Windows Forms Application in C#. I need to be able to bring it to the foreground. After some Googling and experimentation, I have a working solution that looks pretty hacky.
I would like to know the elegant way to do this, if there is one. I need the app to restore and come to the foreground whether it was minimized, or not minimized but in background.
Current code looks like this:
WindowState = FormWindowState.Minimized; WindowState = FormWindowState.Normal; BringToFront(); Focus();
-
D'Arcy Rittich over 14 yearsHmm, that doesn't win any prizes for elegant, is there not a good native .Net way?
-
D'Arcy Rittich over 14 yearsThis does not work for me, at least not in the Visual Studio host debug environment.
-
Jacob Seleznev over 14 yearsFrom MSDN: "The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates the window". With this your application can force the window to the foreground, while the user working with another application.
-
Jacob Seleznev over 14 yearsI just tried this. Activate() will not work if the form is an MDI child form and the user is working with another application.
-
Jon Cage over 9 yearsDoEvents can lead to all sorts of nastiness - I'd strongly advise against using it.
-
Joel Coehoorn over 9 years@JonCage Definitely agree: stackoverflow.com/questions/11352301/…
-
Robb Sadler over 9 yearsAlso useful if you minimize to the System Tray and need to activate and bring to front on click when window is already in Normal state.
-
Xan-Kun Clark-Davis over 8 yearsMaybe it's not beautiful, but in my (very specific) case, that was indeed the way to go. Thanks a lot for bringing this "up2 :-)
-
Giles almost 8 yearsI also found this useful for restoring the form to its prior state (normal or maximised).
-
Imran Ali Khan over 7 yearsPlease add some explanation to your answer. Code-only answers are discouraged on SO.
-
cortexm_0 over 7 yearsCalling form.ShowDialog() instead of form.show() will puts the form on the foreground.
-
cortexm_0 over 7 yearsAnd in case of the mainform was set mainform.TopMost with true, it is better to set form.TopMost = true before u call the form.ShowDialog().
-
BloodyRain2k over 7 yearsIf DoEvents is so evil then maybe M$ should have made sure that Show or BringToFront actually work so we don't have to rely on DoEvents for that. I had the same issue that a window I sent to the tray was always in the background when coming back and nothing but this worked.
-
Daniel Möller almost 7 yearsCuriously, in one machine I could use just
this.Activate()
. And it wouldn't work in another machine (theoretically the same Windows 7 version). This answer solved the problem. -
John almost 7 years@Daniel, Agreed! The other answers did not work nearly as well for me, as this approach.
-
CodingBarfield over 6 yearsThis works for me, all other solutions did not work.
-
Yet Another Code Maker almost 6 yearsGreat !! The simplest and most efficient (at least in my case).
-
Robin Bennett over 5 yearsI found that on some machines Activate merely made the taskbar icon flash.
-
Robin Bennett over 5 yearsI found that even after setting TopMost back to false, the window would remain on top after the user clicked away (unless you click a menu), until it had received at least one click
-
Missy over 5 yearsThanks for this. It is SO ANNOYING that I had to resort to this. So appreciate the help :)
-
Elmue about 5 yearsUsing TopMost is deprecated! Never use this. Your users will hate you.
-
Appleoddity over 3 yearsThe problem with this is that the window is minimized and then brought back in to view. Because I am unable to reliably detect if the form is visible on the screen, this is messy in cases where the form is already visible on the screen.
-
John over 3 years@Appleoddity, I've not noticed flickering with this approach in our GUI's. However, if you're getting flickering that totally sucks, and is worth a new question IMHO with a reproducible code demonstrating it.