Run program in sys tray

  • Thread starter Thread starter Casper
  • Start date Start date
C

Casper

I'm trying to get my program to run in the system tray.
I've looked at dozend of tutorials on the web and on newsgroups but I
just dont get it!

Has anyone got any links or example code that could use?

I'm using Microsoft Visual Basic 2005 Express Edition.

Thanks in advance.

Casper.
 
Casper said:
I'm trying to get my program to run in the system tray.
I've looked at dozend of tutorials on the web and on newsgroups but I
just dont get it!

Has anyone got any links or example code that could use?


Take a look at the toolbox. You'll find a NotifyIcon component on the
"Windows Forms" tab which you can drag onto the form. Assign an icon to its
'Icon' property and set its 'Visible' property to 'True'. This will show
the icon in the taskbar's notification area.
 
Thanks Herfried. I didnt know it would be THAT easy!

OK, I've got my Icon showing in the sys tray. When the program loads I
want it to minimize to the tray and not to be in the task bar. On the
icon I've got to buttons: Open and Exit. When you click open I want it
to open the form and show in the task bar, and when you click exit I
want the program to quit. How would I go about doing this?

Casper
 
Casper,
In addition to the other comments.

It sounds like you used a Form as the startup object of your project, which
you hide, to allow having a NotifyIcon in the system tray. Correct?

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
 

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