Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; BringToFront(); Focus(); 
like image 217
D'Arcy Rittich Avatar asked Sep 23 '09 00:09

D'Arcy Rittich


People also ask

How can I bring my application window to the front?

"Activate" does make Windows bringing current form to front of any other application currently open within the O/S. On the other hand, "bring to front" only make desired form showing in front of other forms within current application (not O/S). Form.

Which method starts the Windows Forms application and displays the specified form on the screen?

The Run method starts the application. It begins running a standard application message loop on the current thread, and makes the specified form visible.

Which of the following method is used to close the windows form application?

The Exit method stops all running message loops on all threads and closes all windows of the application.


1 Answers

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. :)

like image 118
bobbymcr Avatar answered Sep 29 '22 02:09

bobbymcr