2

I have been having issues trying to get my form to minimize into the notification area.

The following code continues to run but my form disappears from the task bar and will not show up.

protected override void OnResize(EventArgs e)
{

    base.OnResize(e);
    bool cursorNotInBar = Screen.GetWorkingArea(this).Contains(Cursor.Position);
    if(this.WindowState == FormWindowState.Minimized && cursorNotInBar)
    {
        this.ShowInTaskbar = false;
        notifyIcon1.Visible = true;
        this.Hide();
    }

How can I go about fixing this?

matt.
  • 2,355
  • 5
  • 32
  • 43
MiekShreds
  • 27
  • 4
  • You forgot to do something to make it visible again, [click](http://stackoverflow.com/q/6317033/1997232). – Sinatr Dec 14 '15 at 15:34
  • @Sinatr I tried that before but I got this error: Error 2 'WindowsFormApplication3.Form1' does not contain a definition for 'notifyIcon1_MouseDoubleClick' and no extension method 'notifyIcon1_MouseDoubleClick' accepting a first argument of type 'WindowsFormApplication3.Form1' could be found (are you missing a using directive or an assembly reference?) C:\Users\\*******\Downloads\Program\Form1.Designer.cs 321 98 WindowsFormsApplication3 – MiekShreds Dec 14 '15 at 15:38
  • 1
    You are expressly forcing it to not be shown in the task bar, what would you expect?! Just set this property to true in the click or double click event of the notifyIcon and problem solved. – varocarbas Dec 14 '15 at 15:42
  • @varocarbas Error 2 Type 'WindowsFormApplication3.Form1' already defines a member called 'notifyIcon1_MouseDoubleClick' with the same parameter types C:\Users\\********\Downloads\Program\Form1.Designer.cs 360 18 WindowsFormsApplication3 – MiekShreds Dec 14 '15 at 15:45

1 Answers1

2

There are several pretty unpleasant interactions in this code. Starting with the ShowInTaskbar property, it has many side-effects because it forces the native window to be re-created. Just don't tinker with it, there is no need since the taskbar button is already hidden when you hide your window. The NotifyIcon.MouseDoubleClick event is also cranky, you have to restore the window just right to prevent it from staying hidden with a 0x0 size.

Do it like this:

    protected override void OnResize(EventArgs e) {
        base.OnResize(e);
        if (this.WindowState == FormWindowState.Minimized && !notifyIcon1.Visible) {
            notifyIcon1.Visible = true;
            this.Hide();
        }
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
        this.Show();
        this.WindowState = FormWindowState.Normal;
        notifyIcon1.Visible = false;
        this.BringToFront();
    }

The placement of the Show() method is critical. If you move it after the WindowState assignment then the window won't restore properly.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536