Results 1 to 13 of 13

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)

    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

  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:
    VB Code:
    1. Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    2.     Select Case e.CloseReason
    3.         Case CloseReason.ApplicationExitCall
    4.             'The user perfromed an action that caused your code to call Application.Exit.
    5.             'You could go either way here but I'd suggest that you shouldn't be calling Application.Exit
    6.             'if you don't actually want the application to exit.  In that case close the main form instead.
    7.             e.Cancel = False
    8.         Case CloseReason.FormOwnerClosing
    9.             'This form is a modeless dialogue, which you shouldn't be minimising to the system tray anyway.
    10.             e.Cancel = False
    11.         Case CloseReason.MdiFormClosing
    12.             'This form is an MDI child form, which you shouldn't be minimising to the system tray anyway.
    13.             e.Cancel = False
    14.         Case CloseReason.None
    15.             'If the reason can't be determined then something funky is going on so I'd suggest you let the form close.
    16.             e.Cancel = False
    17.         Case CloseReason.TaskManagerClosing
    18.             'The user pressed the End Task button on the Applications tab (NOT the Processes tab) of the Task Manager.
    19.             '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
    20.             'this way.  I'd suggest letting the form close but there would definitely be legitimate reasons for preventing it.
    21.             e.Cancel = False
    22.         Case CloseReason.UserClosing
    23.             'The user clicked the Close button on the title bar, pressed Alt+F4, selected Close from the
    24.             'system menu or performed some action that caused your code to call the form's Close method.
    25.             'Don't let the form close.
    26.             e.Cancel = True
    27.             Me.Visible = False
    28.             Me.NotifyIcon1.Visible = True
    29.         Case CloseReason.WindowsShutDown
    30.             'Windows is shutting down.
    31.             'Definitely let the form close or you'll prevent Windows shutting down normally.
    32.             e.Cancel = False
    33.     End Select
    34. End Sub
    Last edited by jmcilhinney; Feb 24th, 2007 at 10:33 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

  3. #3
    New Member
    Join Date
    Mar 2007
    Posts
    3

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

    Hey a quick question. On the closereason.taskmanagerclose, how could you make it to where you dont get a End Now Window?

    When I tried it out it would work, and would not close, but then the End Program Now came up, and you have to clikc cancel for it to continue. I would like to bypass this if possible.

    Great code though, I have been looking for something like this for a month or so. Thanks

  4. #4

    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)

    Quote Originally Posted by KillerTomato
    Hey a quick question. On the closereason.taskmanagerclose, how could you make it to where you dont get a End Now Window?

    When I tried it out it would work, and would not close, but then the End Program Now came up, and you have to clikc cancel for it to continue. I would like to bypass this if possible.

    Great code though, I have been looking for something like this for a month or so. Thanks
    It's not possible because that's the way the task manager works. In .NET terms it sends a CloseMainWindow command to your process first, then checks to see if it was obeyed. If it wasn't it asks the user whether they still want to close the app by force and, if they do, it sends a Kill command to your process, which cannot be ignored. The only way I can see to get around that would be, once you receive the CloseMainWindow command and refuse it, to use API functions to continually look for the End Task dialogue that the Task Manager displays and remotely "click" its Cancel button.
    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

  5. #5
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

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

    Me.NotifyIcon1.Visible = True

    is that won't work in vb2005? it said that notifyIcon is not a member in my program when debug

    and...
    If e.CloseReason = CloseReason.TaskManagerClosing Then
    e.Cancel = True
    End If

    I find that e cannot being cancel
    Last edited by newpat; Jul 15th, 2007 at 06:24 AM.

  6. #6

    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)

    Quote Originally Posted by newpat
    Me.NotifyIcon1.Visible = True

    is that won't work in vb2005? it said that notifyIcon is not a member in my program when debug

    and...
    If e.CloseReason = CloseReason.TaskManagerClosing Then
    e.Cancel = True
    End If

    I find that e cannot being cancel
    NotifyIcon1 is only a member of your form if you've added NotifyIcon1. If you haven't then it isn't. The original point of this thread was to minimise a form to the system tray instead of closing, which is what the NotifyIcon is for. If that's not what you're doing then ignore that part.

    The whole point of the FormClosing event is so that you CAN cancel the close if you want to. If you weren't able to set e.Cancel then the event would be completely useless.
    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

  7. #7
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

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

    Quote Originally Posted by jmcilhinney
    NotifyIcon1 is only a member of your form if you've added NotifyIcon1. If you haven't then it isn't. The original point of this thread was to minimise a form to the system tray instead of closing, which is what the NotifyIcon is for. If that's not what you're doing then ignore that part.

    The whole point of the FormClosing event is so that you CAN cancel the close if you want to. If you weren't able to set e.Cancel then the event would be completely useless.
    haha, actually, I planned to ask something like your topic idea later...
    I find the msdn with NotifyIcon1, it told me also to use NotifyIcon1, but it did not tell me how to dim it

  8. #8
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

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

    This just using in closing. how about clicking minimise button to?
    How to declare the type of NotifyIcon1?

  9. #9

    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)

    Quote Originally Posted by newpat
    This just using in closing. how about clicking minimise button to?
    Handle the SizeChanged event and test the WindowState property to decide whether to show the form or the tray icon.
    Quote Originally Posted by newpat
    How to declare the type of NotifyIcon1?
    You add a NotifyIcon to your form from the Toolbox.
    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

  10. #10
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

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

    It is very successful to system tray. But It cannot be re-open from the system tray.

    vb Code:
    1. Private Sub Main_FormMinize(ByVal sender As Object, _
    2.     ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.MinimumSizeChanged
    Its not work

  11. #11
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

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

    Quote Originally Posted by newpat
    It is very successful to system tray. But It cannot be re-open from the system tray.

    vb Code:
    1. Private Sub Main_FormMinize(ByVal sender As Object, _
    2.     ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.MinimumSizeChanged
    Its not work
    Handle the Click or DoubleClick event of the notify icon to show your form.

    Another way to do it is to make a context menu and attach it to your notify icon, then you can have them open various different windows (or just the main window)
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  12. #12
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

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

    It's a nice code John. I didn't knew that such thing is also introduced with .NET 2.0. I remember that when I was developing a xmpp messenger client for my company with .NET 1.1, I trapped the windows messages for this. When user were trying to Shutdown the Windows while running my messenger client in system tray, they were not able to shutdown the system. After some time I realized that When user was clicking on the X button of main window, I was canceling the form closing and hiding the window to system tray. It happened because I trapped the wrong windows message. It was a pain really. Now life is easy. Thanks for sharing this.

  13. #13
    New Member
    Join Date
    Oct 2009
    Posts
    7

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

    This is what I was looking for!

    Thanks.

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