What is the "right" way to bring a Windows Forms Application to the foreground?

42,736

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();
Share:
42,736
D'Arcy Rittich
Author by

D'Arcy Rittich

Updated on April 23, 2020

Comments

  • D'Arcy Rittich
    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
    D'Arcy Rittich over 14 years
    Hmm, that doesn't win any prizes for elegant, is there not a good native .Net way?
  • D'Arcy Rittich
    D'Arcy Rittich over 14 years
    This does not work for me, at least not in the Visual Studio host debug environment.
  • Jacob Seleznev
    Jacob Seleznev over 14 years
    From 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
    Jacob Seleznev over 14 years
    I 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
    Jon Cage over 9 years
    DoEvents can lead to all sorts of nastiness - I'd strongly advise against using it.
  • Joel Coehoorn
    Joel Coehoorn over 9 years
    @JonCage Definitely agree: stackoverflow.com/questions/11352301/…
  • Robb Sadler
    Robb Sadler over 9 years
    Also 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
    Xan-Kun Clark-Davis over 8 years
    Maybe 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
    Giles almost 8 years
    I also found this useful for restoring the form to its prior state (normal or maximised).
  • Imran Ali Khan
    Imran Ali Khan over 7 years
    Please add some explanation to your answer. Code-only answers are discouraged on SO.
  • cortexm_0
    cortexm_0 over 7 years
    Calling form.ShowDialog() instead of form.show() will puts the form on the foreground.
  • cortexm_0
    cortexm_0 over 7 years
    And 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
    BloodyRain2k over 7 years
    If 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
    Daniel Möller almost 7 years
    Curiously, 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
    John almost 7 years
    @Daniel, Agreed! The other answers did not work nearly as well for me, as this approach.
  • CodingBarfield
    CodingBarfield over 6 years
    This works for me, all other solutions did not work.
  • Yet Another Code Maker
    Yet Another Code Maker almost 6 years
    Great !! The simplest and most efficient (at least in my case).
  • Robin Bennett
    Robin Bennett over 5 years
    I found that on some machines Activate merely made the taskbar icon flash.
  • Robin Bennett
    Robin Bennett over 5 years
    I 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
    Missy over 5 years
    Thanks for this. It is SO ANNOYING that I had to resort to this. So appreciate the help :)
  • Elmue
    Elmue about 5 years
    Using TopMost is deprecated! Never use this. Your users will hate you.
  • Appleoddity
    Appleoddity over 3 years
    The 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
    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.