Tuesday, July 19, 2011 | By: nika perales

How to Minimize Your VB.Net Application to the System Tray

How to Minimize Your VB.Net Application to the System Tray

If you want your VB.Net application to minimize to the system tray instead of closing, then this sample project will help you. First create a windows forms application, Then add the following declarations to your forms code.
Private TrayIcon As NotifyIcon
Private TrayMenu As ContextMenuStrip
Private AppClose As Boolean   'Boolean FLAG which identifies 
                              'that you want to exit the application


The TrayIcon will hold all the information of the type of icon that you want to appear on the system tray. This includes the icon that would appear on the system tray, the text that will display when you hover on the tray icon, as well as the context menu for the tray icon.

Now Add the following code on your Forms Load Event, This will populate the contextmenu with items and add the corresponding click event handler.

 Dim mnuShowForm, mnuExit As ToolStripMenuItem
 Dim mnuSep As New ToolStripSeparator

 TrayMenu = New ContextMenuStrip

 mnuShowForm = New ToolStripMenuItem("Open Form")
 AddHandler mnuShowForm.Click, AddressOf MenuItemClick

 mnuExit = New ToolStripMenuItem("Exit Program")
 AddHandler mnuExit.Click, AddressOf MenuItemClick

 TrayMenu.Items.AddRange(New ToolStripItem() {mnuShowForm, mnuSep, mnuExit})

After that, create the procedure below

  Private Sub MenuItemClick(ByVal sender As Object, ByVal e As System.EventArgs)
        Select Case CType(sender, ToolStripMenuItem).Text
            Case "Open Form"
                TrayIcon.Visible = False
                Me.Show()

            Case "Exit Program"
                If Not IsNothing(TrayIcon) Then
                    ShowTrayIcon(False)
                    TrayIcon = Nothing
                    AppClose = True 'tell the program to exit
                                    'this prevents the form
                                    'from hiding itself
                                    'instead of closing
                End If
                Application.Exit()
        End Select
    End Sub

The above code will handle the events fired when you click on the contextmenu items of of your TrayIcon.
Now add the following code which handles the creation of the NotifyIcon Object as well as showing and hiding of the tray icon itself.
  Private Sub ShowTrayIcon(Optional ByVal blnShow As Boolean = True)
        If TrayIcon Is Nothing Then
            TrayIcon = New NotifyIcon
            TrayIcon.Icon = Me.Icon
            TrayIcon.ContextMenuStrip = TrayMenu
            TrayIcon.Text = Me.Text
        End If

        If blnShow Then
            TrayIcon.Visible = True
        Else
            TrayIcon.Visible = False
        End If
    End Sub

Finally, add the following code on your forms FormClosing event. This will trigger the showing of the tray icon and hiding the form from view.
Note: setting E.Cancel = True instructs the system that we want to cancel the closing of the form.

Attached is the completed sample project. Feel Free to send me any questions that you may have...

Download Code

0 comments:

Post a Comment