How can I bring my application window to the front? [duplicate]
Solution 1:
Here is a piece of code that worked for me
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;
It always brings the desired window to the front of all the others.
Solution 2:
Use Form.Activate()
or Form.Focus()
methods.
Solution 3:
While I agree with everyone, this is no-nice behavior, here is code:
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
SetForegroundWindow(Handle.ToInt32());
Update
David is completely right, for completeness I include the list of conditions that must apply for this to work (+1 for David!):
- The process is the foreground process.
- The process was started by the foreground process.
- The process received the last input event.
- There is no foreground process.
- The foreground process is being debugged.
- The foreground is not locked (see LockSetForegroundWindow).
- The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
- No menus are active.
Solution 4:
Use Control.BringToFront
:
myForm.BringToFront();
Solution 5:
this works:
if (WindowState == FormWindowState.Minimized)
WindowState = FormWindowState.Normal;
else
{
TopMost = true;
Focus();
BringToFront();
TopMost = false;
}