System Tray - Notification Area - Visual Basic .NET
Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013
Start an new project
You can also add an icon to your application. I have added also a label and changed its text property to System Tray:
Go to the toolbox and a ContextMenuStrip:
Fill up to important menus inside the first 2 boxex: Show and Exit as shown above.
You can add more menus depends on the project you are working on.
Add a NotifyIcon to the form:
Once you add NotifyIcon, you should have 2 icons in gray area: ContextMenuStrip and NotifyIcon as below:
It is important that you add an icon to the NotifyIcon so that when the program is running in the Notification Area you will be able to recognize it.
To do that right click on NotifyIcon in the gray area then click on properties:
Then go to Properties window on the and click next to Icon. Browse your computer to your desired icon.
On the same window, change the ContextMenuStrip to ContextMenuStrip1:
Let's now start adding the codes to the program:
First we should make sure that ContextMenuStrip is not enabled when the program starts:
Double click on the form and that should take you to the code page:
Add the following load event:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ContextMenuStrip1.Enabled = False End Sub
Then Add the following code so that when the form closes by the user, it stays running in the notification area:
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing 'Cancel Closing: e.Cancel = True 'Minimize the form: Me.WindowState = FormWindowState.Minimized 'Don't show in the task bar Me.ShowInTaskbar = False 'Enable the Context Menu Strip ContextMenuStrip1.Enabled = True End Sub
Then add a code to the Show Menu of the ContextMenuStip1:
Private Sub ShowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowToolStripMenuItem.Click 'When Show menu clicks, it will show the form: Me.WindowState = FormWindowState.Normal 'Show in the task bar: Me.ShowInTaskbar = True 'Disable the Context Menu: ContextMenuStrip1.Enabled = False End Sub
Add a Code to the Exit menu so that when it clicks it the program exits:
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click End End Sub
Add a code so that when the form minimizes, it disappears from the task bar, and run in the notification area:
Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged If Me.WindowState = FormWindowState.Minimized Then ShowInTaskbar = False ContextMenuStrip1.Enabled = True End If End Sub
Run the program and you should see an icon in the notification area for your project.