Share via


VB.NET: How to Make a Right Click Menu for a Tray Icon

Some of you (like me), probably noticed that a ContextMenuStrip ("Right Click Menu") doesn't close automatically if you click outside it and it isn't in a form (in Visual Basic 2008, but i don't know about the later versions). So in this article I'll explain how to solve that "bug".
We'll be using as example a ContextMenuStrip on a NotifyIcon ("Tray Icon"), because I think that is the most common use of a ContextMenuStrip outside a form.

In this article:


 Steps

Adding objects

1: Adding a NotifyIcon

Add a NotifyIcon to your main Form or Module (if you didn't do that already), and name it whatever you want (for this article we will call it "tray").

2: Adding a Form

Add a Form to your project, and name it whatever you want (for this article we will call it "trayform").

3: Adding a ContextMenuStrip

Add a ContextMenuStrip to your new Form ("trayform"), and name it whatever you want (for this article we will call it "traymenu").

Setting up the objects

1: Setting up the NotifyIcon

In the Properties set Icon to the icon you want to appear in the tray.
Set text to whatever you want (for this article we will set it to "Tray App").
Leave the other Properties as they are.

2: Setting up the Form

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.

3: Setting up the ContextMenuStrip

Add items to the ContextMenuStrip (for this article we will just add one item called "Exit").
Leave the Properties as default.

Writing the code

1: Writing the code on the main Form or Module

Select "tray" and in events double-click on "MouseClick" to create a "MouseClick" event, then put the following code in the event. (don't forget to properly rename the objects in the code if you didn't use the same names as in this article.)

If e.Button = Windows.Forms.MouseButtons.Right Then 'Checks if the pressed button is the Right Mouse
    trayform.Show() 'Shows the Form that is the parent of "traymenu"
    trayform.Activate() 'Set the Form to "Active", that means that that will be the "selected" window
    trayform.Width = 1 'Set the Form width to 1 pixel, that is needed because later we will set it behind the "traymenu"
    trayform.Height = 1 'Set the Form Height to 1 pixel, for the same reason as above
End If

2: Writing the code on the "trayform"

2.1: Load event

Double-click on the "trayform" Form, to create a Load event, and put the following code in the event. (don't forget to properly rename the objects in the code if you didn't use the same names as in this article.)

traymenu.Show(Cursor.Position) 'Shows the Right click menu on the cursor position
Me.Left = traymenu.Left + 1 'Puts the form behind the menu horizontally
Me.Top = traymenu.Top + 1 'Puts the form behind the menu vertically
2.2: Deactivate event

Select the "trayform" Form, and in events double-click on "Deactivate", to create a "Deactivate" event. This event happens when you click outside the form or another window goes to front, what means that the "trayform" Form will not longer be active.
Next put the following code into the event.

Me.Close() 'Closes the "trayform" Form and consequently every thing in it, including the "traymenu"
2.3 Exit button click event

This event is just for the item we added to the "traymenu" so if you added different items to it you can ignore this, and write your own code.
Select the "traymenu" and then the "Exit" option in it, in events double-click on "Click" to add a click event. And put the following code in the event.
NOTE: If you are using a Module as startup object instead of a Form, just remove "Form1." from the starting of the first line.

Form1.tray.Visible = False  'Hides the tray icon. if we don't do this we can kill the app, but the icon will still be there
End 'Kills the application
↑​Return to Top

Conclusion

Now if you run your program, you can right-click on the tray icon and close your app, or just click outside of it and the menu will magically disappear!
I hope this was helpful.

Return to Top