RedFilter RedFilter - 3 years ago 203
C# Question

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

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;

Answer Source

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;


Warning: this is annoying! If it's just an app for your personal use, as you say, maybe you can live with it. :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download