How to make a Windows Forms .NET application display as tray icon?
You can add the NotifyIcon component from the toolbox onto your main form.
This has events such as MouseDoubleClick that you can use to handle various events.
Edit: You have to make sure that you set the Icon property to a valid .ico file if you want it to show up properly in the systray.
First, add a NotifyIcon control to the Form. Then wire up the Notify Icon to do what you want.
If you want it to hide to tray on minimize, try this.
Private Sub frmMain_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.ShowInTaskbar = False
Else
Me.ShowInTaskbar = True
End If
End Sub
Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
Me.WindowState = FormWindowState.Normal
End Sub
I'll occasionally use the Balloon Text in order to notify a user - that is done as such:
Me.NotifyIcon1.ShowBalloonTip(3000, "This is a notification title!!", "This is notification text.", ToolTipIcon.Info)
To extend Tom's answer, I like to only make the icon visible if the application is minimized.
To do this, set Visible = False
for NotifyIcon and use the below code.
I also have code below to hide the icon during close the prevent the annoying ghost tray icons that persist after application close.
Private Sub Form_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If Me.WindowState = FormWindowState.Minimized Then
Hide()
NotifyIcon1.Visible = True
NotifyIcon1.ShowBalloonTip(3000, NotifyIcon1.Text, "Minimized to tray", ToolTipIcon.Info)
End If
End Sub
Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
Show()
Me.WindowState = FormWindowState.Normal
Me.Activate()
NotifyIcon1.Visible = False
End Sub
Private Sub Form_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
NotifyIcon1.Visible = False
Dim index As Integer
While index < My.Application.OpenForms.Count
If My.Application.OpenForms(index) IsNot Me Then
My.Application.OpenForms(index).Close()
End If
index += 1
End While
End Sub
If you want to add a right click menu:
VB.NET: How to Make a Right Click Menu for a Tray Icon
Per the article (with mods for context):
Setting up the Form for hosting the tray icon context menu
- In the Properties set FormBorderStyle to None.
- Set ShowInTaskbar as False (because we don't want an icon appearing in taskbar when we right-click the tray icon!).
- Set StartPosition to Manual.
- Set TopMost to True.
- Add a ContextMenuStrip to your new Form, and name it whatever you want.
- Add items to the ContextMenuStrip (for this example just add one item called "Exit").
The Form code behind will look like this:
Private Sub Form_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate
Me.Close()
End Sub
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ContextMenuStrip1.Show(Cursor.Position)
Me.Left = ContextMenuStrip1.Left + 1
Me.Top = ContextMenuStrip1.Top + 1
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
MainForm.NotifyIcon1.Visible = False
End
End Sub
I then change the notifyicon mouse event to this (TrayIconMenuForm
is the name of my Form for providing the context menu):
Private Sub NotifyIcon1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
Select Case e.Button
Case Windows.Forms.MouseButtons.Left
Show()
Me.WindowState = FormWindowState.Normal
Me.Activate()
NotifyIcon1.Visible = False
Case Windows.Forms.MouseButtons.Right
TrayIconMenuForm.Show() 'Shows the Form that is the parent of "traymenu"
TrayIconMenuForm.Activate() 'Set the Form to "Active", that means that that will be the "selected" window
TrayIconMenuForm.Width = 1 'Set the Form width to 1 pixel, that is needed because later we will set it behind the "traymenu"
TrayIconMenuForm.Height = 1 'Set the Form Height to 1 pixel, for the same reason as above
Case Else
'Do nothing
End Select
End Sub