Results 1 to 13 of 13

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

Threaded View

  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)

    C# 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:
    VB Code:
    1. Private Sub Form1_FormClosing(ByVal sender As Object, _
    2.                               ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    3.     If e.CloseReason = CloseReason.UserClosing Then
    4.         'The user has requested the form be closed so mimimise to the system tray instead.
    5.         e.Cancel = True
    6.         Me.Visible = False
    7.         Me.NotifyIcon1.Visible = True
    8.     End If
    9. End Sub
    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.


    I only just discovered the CloseReason property myself, so I thought I'd share it as others may not be aware of it either.
    Last edited by jmcilhinney; Oct 30th, 2008 at 08:25 PM.
    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