Search code examples
vb.netsystem-tray

VB.net tray icon creation


I am dynamically creating a NotifyIcon using the code below:

Dim traysystem As New NotifyIcon 

Question being, how can I create a click event for that?

I've tried:

Private Sub traysystem_click(ByVal sender As System.Object, ByVal e As  _
System.EventArgs) Handles traysystem.Click
    messagebox.show("test!")
End Sub

But yeah, doesn't work.


Solution

  • Handles requires WithEvents in the DIM statement:

     Dim WithEvents traysystem As New NotifyIcon 
    

    The scope of this declaration isn't obvious from the snippet. If it is a local variable then you have to use AddHandler and remove the Handles keyword:

        Dim traysystem As New NotifyIcon
        AddHandler traysystem.Click, AddressOf traysystem_click