Context menu help

nbrege

Freshman
Joined
Jan 12, 2006
Messages
34
I have a notify icon in the system tray for my application. When I right click the icon, a menu pops up with 3 items to pick. Here's where I'm stuck - how do I make my program do something when I pick one of the 3 items on the menu? ie. what is the event to catch the click on a context menu? Here is my code so far:

Visual Basic:
Public Class Form1

 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Initalize the NotifyIcon object's shortcut menu.

Dim menuList() As MenuItem = New MenuItem() {New MenuItem("Menu 1"), New MenuItem("Menu 2"), New MenuItem("Exit")}

Dim clickMenu As New ContextMenu(menuList)

NotifyIcon1.ContextMenu = clickMenu

End Sub

Private Sub TrayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrayButton.Click

NotifyIcon1.Icon = System.Drawing.SystemIcons.Information

NotifyIcon1.Text = "Tray Test"

NotifyIcon1.Visible = True

Me.Hide()

End Sub

Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick

NotifyIcon1.Visible = False

Me.Show()

End Sub

Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click

NotifyIcon1.Visible = False

Me.Close()

End Sub


End Class


Thanks for any help...
 
You'll need to add action handlers to your class that handle the click event for Menu 1, Menu 2, and Menu 3. To do this, instead of instantiating the menu items on the fly and not keeping a pointer to them, you will need to keep a pointer.

I haven't used a contect menu in a while, but if I recall correctly, there was a way to add the menu items from the designer. Try adding the context menu in the designer instead of in code. You should be able to add Menu 1, 2, and 3 in the designer. That will take care of everything I mentioned above. Then just double click on each menu item and it should make the event method stub for you.
 
Back
Top