Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Windows Forms .NET application display as tray icon?

What needs to be done to have your .NET application show up in Window's system tray as icon?

And how do you handle mousebutton clicks on said icon?

like image 274
steffenj Avatar asked Oct 01 '08 18:10

steffenj


People also ask

How do I create a system tray?

Step 1 − Go to the SETTINGS window and choose System. Step 2 − In the SYSTEM window, select Notifications & actions. Here you can select the option that reads “Select which icons appear on the taskbar”.


2 Answers

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)
like image 140
tom.dietrich Avatar answered Oct 18 '22 07:10

tom.dietrich


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.

like image 30
Carl Avatar answered Oct 18 '22 07:10

Carl