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();
"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.
The Run method starts the application. It begins running a standard application message loop on the current thread, and makes the specified form visible.
The Exit method stops all running message loops on all threads and closes all windows of the application.
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With