Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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...

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...