INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Toggle between minimize to tray or taskbar (but not both)

Toggle between minimize to tray or taskbar (but not both)

(OP)
I have a form with a fixed dialog border; it can either be on-screen normally or minimized. I want to allow the user to (optionally) minimize it to the tray. To that end I have a context menu with a menu item "Minimize to tray" which toggles its check mark when you click it.

private bool m_MinimizeToTray;

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
   minimizeToTrayToolStripMenuItem.Checked = m_MinimizeToTray;
}

private void minimizeToTrayToolStripMenuItem_Click(object sender, EventArgs e)
{
   m_MinimizeToTray = !m_MinimizeToTray;
} 

Then in the form's Resize event handler I tackle minimizing to the tray (based upon the state of the Boolean) and restoring (and simultaneously hiding the tray icon):

private void Form1_Resize(object sender, EventArgs e)
{
   if(WindowState == FormWindowState.Minimized && m_MinimizeToTray)
   {
      m_NotifyIcon.Visible = true;
      Hide();
   }
   else
   {
      m_NotifyIcon.Visible = false;
      Show();
   }
} 
Finally, I wanted to restore to normal size when the user left-clicks the tray icon:
private void m_NotifyIcon_MouseUp(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Left)
   {
      WindowState = FormWindowState.Normal;
      Show();
   }
} 

It almost works, but only almost. You have to click the tray icon twice to restore the window. The first click restores the icon in the task bar, the second click restores the app. To make matters worse, if you single click the tray icon and then click the task bar icon, it gets into a state where you can't ever restore the window. Any ideas how to get this to work properly (so that a single click on the tray icon will restore the window)?

RE: Toggle between minimize to tray or taskbar (but not both)

(OP)
I figured it out. Apparently I needed to call Show() before setting the WindowState to Normal. The following works:

private void m_NotifyIcon_MouseUp(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Left)
   {
      Show();
      WindowState = FormWindowState.Normal;
   }
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close