You never display anything in the notification area. Trace through your code and try to see what's happening. I've added some comments:
private void button6_Click(object sender, EventArgs e)
{
// When button 6 is clicked, minimize the form.
this.WindowState = FormWindowState.Minimized;
}
private void Form_Resize(object sender, EventArgs e)
{
// Catch the case where the form is minimized, including but not limited to
// clicks on button 6.
if (WindowState == FormWindowState.Minimized)
{
// In that case, hide the form.
this.Hide();
}
}
private void notifyIcon_Click(object sender, EventArgs e)
{
// If the notification icon is clicked, reshow the form as a normal window.
this.Show();
this.WindowState = FormWindowState.Normal;
}
Notice the problem now? When the form is minimized, all you do is hide it. You never tell the NotifyIcon
to display its icon! The default value of its Visible
property is false
. You have to set it to true
to make the icon show up, and false
to get it to go away.
So modify your code as follows:
private void Form_Resize(object sender, EventArgs e)
{
// Catch the case where the form is minimized, including but not limited to
// clicks on button 6.
if (WindowState == FormWindowState.Minimized)
{
// In that case, hide the form.
this.Hide();
// And display the notification icon.
notifyIcon.Visible = true;
// TODO: You might also want to set other properties on the
// notification icon, like Text and/or Icon.
}
}
private void notifyIcon_Click(object sender, EventArgs e)
{
// If the notification icon is clicked, reshow the form as a normal window.
this.Show();
this.WindowState = FormWindowState.Normal;
// And hide the icon in the notification area.
notifyIcon.Visible = false;
}