Results 1 to 2 of 2

Thread: Minimise Form to System Tray on Close (.NET 2.0)

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Minimise Form to System Tray on Close (.NET 2.0)

    VB version here.

    In previous versions minimising to the system tray instead of closing a form was a hassle. You had to mess about checking whether the system was shutting down, etc. or else your app would prevent Windows from ever closing. In .NET 2.0 that process is simplified consideralbly:
    CSharp Code:
    1. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    2. {
    3.     if (e.CloseReason == CloseReason.UserClosing)
    4.     {
    5.         // The user has requested the form be closed so mimimise to the system tray instead.
    6.         e.Cancel = true;
    7.         this.Visible = false;
    8.         this.notifyIcon1.Visible = true;
    9.     }
    10. }
    This will minimise the form to the system tray if the user requests the close by clicking the Close button on the title bar, selecting Close from the system menu, pressing Alt+F4 or performing some action that causes your code to call the form's Close method. All other reasons for closure, including calling Application.Exit or Windows shutting down, will cause the form to actually close.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Minimise Form to System Tray on Close (.NET 2.0)

    Here's a more thorough look at the different reasons a form could be closing:
    CSharp Code:
    1. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    2. {
    3.     switch (e.CloseReason)
    4.     {
    5.         case CloseReason.ApplicationExitCall:
    6.             // The user perfromed an action that caused your code to call Application.Exit.
    7.             // You could go either way here but I'd suggest that you shouldn't be calling Application.Exit
    8.             // if you don't actually want the application to exit.  In that case close the main form instead.
    9.             e.Cancel = false;
    10.             break;
    11.         case CloseReason.FormOwnerClosing:
    12.             // This form is a modeless dialogue, which you shouldn't be minimising to the system tray anyway.
    13.             e.Cancel = false;
    14.             break;
    15.         case CloseReason.MdiFormClosing:
    16.             // This form is an MDI child form, which you shouldn't be minimising to the system tray anyway.
    17.             e.Cancel = false;
    18.             break;
    19.         case CloseReason.None:
    20.             // If the reason can't be determined then something funky is going on so I'd suggest you let the form close.
    21.             e.Cancel = false;
    22.             break;
    23.         case CloseReason.TaskManagerClosing:
    24.             // The user pressed the End Task button on the Applications tab (NOT the Processes tab) of the Task Manager.
    25.             // You could go either way here too.  It really depends on your app and if you don't want the user to be able to exit
    26.             // this way.  I'd suggest letting the form close but there would definitely be legitimate reasons for preventing it.
    27.             e.Cancel = false;
    28.             break;
    29.         case CloseReason.UserClosing:
    30.             // The user clicked the Close button on the title bar, pressed Alt+F4, selected Close from the
    31.             // system menu or performed some action that caused your code to call the form's Close method.
    32.             // Don't let the form close.
    33.             e.Cancel = true;
    34.             this.Visible = false;
    35.             this.notifyIcon1.Visible = true;
    36.             break;
    37.         case CloseReason.WindowsShutDown:
    38.             // Windows is shutting down.
    39.             // Definitely let the form close or you'll prevent Windows shutting down normally.
    40.             e.Cancel = false;
    41.             break;
    42.     }
    43. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width