system tray icon with popup menu

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I have often seen system tray icons that you click on and a popup menu
appears. Is it possible to do this in Visual basic.net or are there any
controls that allow this to be achieved.

Thanx in advance.

Robert
 
Robert,
Matthew MacDonald's book "Microsoft Visual Basic .NET Programmer's Cookbook"
has a topic on creating a system tray program.

Basically: Rather than using a Form as the startup object, use a Component
instead.

Create a new Component class (use Project - Add Component). Add a NotifyIcon
to the component designer. Also add a ContextMenu object for the
NotifyIcon. When you click the menu, create and show the form. Remember to
put an Exit option on the menu.

Make the Component the startup object, adding a Shared Sub Main to the
component.

Public Class Component1
Inherits System.ComponentModel.Component

' Component designer generated code omitted.

Public Shared Sub Main
Dim app as New Component1
Application.Run()
End Sub

Private Sub menuOptions_Click(...) Handles menuOptions.Click
' this would be your settings dialog.
Dim dialog as New OptionsDialog
dialog.ShowDialog()
dialog.Dispose()
End Sub

Private Sub menuExit_Click(...) Handles menuExit.Click
Me.Dispose()
Application.Exit()
End Sub

End Sub

The problem is you cannot edit the menu from the Component Designer. What I
do is use cut & paste from a Form Designer onto the Component Designer to
get the menu to the component...

The call to Me.Dispose enables the icon to be removed from the system tray
right away rather then waiting.

Hope this helps
Jay
 
There is a simple example in the VB.NET help. Just lookup NotifyIcon. There
is an article called: 'Associating a Context Menu with a Windows Forms
NotifyIcon Component', which is exactly what you want.
 
Crochie,
Obviously if you like work & extra typing you can create a menu completely
by scratch in code as that article suggests.

However! I prefer to use the designers to do the work for me. So my
suggestion of using Cut & Paste seems to be exactly what *I* want :-)

Thanks for the link! I will try to remember to include it with my sample, so
others will have both alternatives.

Hope this helps
Jay
 
I understand what you mean, jay. Sometimes I use the designer & others, I
code it.

When it comes to either coding or using the properties window, I use code
99% of the time. It's the way to do things in my eyes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top