Reputation: 9990
I've tried several things, but none of them work...
I have Form that should come in front of all Windows on clicking NotifyIcon. So here is what I tried:
private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.TopMost = true;
this.BringToFront();
this.Focus();
this.TopMost = false;
}
}
Then I tried to use SetForegroundWindow:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);
by adding
SetForegroundWindow(this.Handle);
at the end of the if block.
Finally, I saw that when this doesn't work if I click right mouse button on NotifyIcon and context menu gets opened, I can then left click NotifyIcon and it brings it to the front.
I've tried to add this code at the beginning:
cmsNotifyIcon.Show();
cmsNotifyIcon.Close();
So that it shows and closes notifyIcon context menu, as a possible idea for workaround, but it doesn't help.
Any ideas on how to do this, or work around this?
Upvotes: 6
Views: 5876
Reputation: 7735
This function works for me:
public static void BringFormToFront(Form form) {
form.Activate();
form.TopMost = true;
form.TopMost = false;
form.Focus();
form.BringToFront();
}
and if you want to be extra lazy:
public static void ShowFormToFront(Form form) {
form.Show();
BringFormToFront(form);
}
These could easily be extension methods.
Upvotes: 0
Reputation: 789
I had a similar problem; a simple workaround sufficed for me. In the event handler where I need to show the form, I simply check to see if the windows is visible/normal, and if it is, then I minimize it. The rest of the code then brings it back up.
private void OnDetails(object Sender, EventArgs Args)
{
if (DetailsForm == null)
{
DetailsForm = new MyForm(this);
}
if (DetailsForm.WindowState == FormWindowState.Normal)
DetailsForm.WindowState = FormWindowState.Minimized;
DetailsForm.WindowState = FormWindowState.Normal;
DetailsForm.Show();
}
Upvotes: 0
Reputation: 2131
Use Activate()
instead of Show(). Also if your form is minimized you have to set it's WindowState to WindowState.Normal (or whatever state it was before minimizing).
private void notifyIcon1_Click(object sender, EventArgs e)
{
Activate();
// this is needed for minimized form to show
WindowState = FormWindowState.Normal;
}
Upvotes: 1
Reputation: 46034
Here's how I've done it. Note that StartupWindowState
and HideWhenMinimized
are a private members of my form.
private void OnOpenTrayMenuItemClicked(object sender, EventArgs e) {
if (this.WindowState == FormWindowState.Minimized) {
this.WindowState = this.StartupWindowState;
this.ShowInTaskbar =
(this.HideWhenMinimized && (this.WindowState == FormWindowState.Minimized)) ? false : true;
this.Show();
}
this.Activate();
}
Upvotes: 3