Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

System Tray Applications

4 views
Skip to first unread message

One Handed Man [ OHM ]

unread,
Sep 17, 2003, 1:29:57 PM9/17/03
to
If this is duplicated, I'm sorry because I cant find the original post I
made today

Anyway . .

Now its my turn to ask a question

I want to develop an app which will run in the system tray. How can I do
this?


Regards OHM


Armin Zingler

unread,
Sep 17, 2003, 1:44:01 PM9/17/03
to
"One Handed Man [ OHM ]"
<terry_burnsREMOVE%FOR%NO%SP...@BTOpenworld.com> schrieb

There is not much space in the system tray, so your app might get hurt. ;-))


Use the NotifyIcon component (System.Windows.Forms.NotifyIcon)


--
Armin

Jay B. Harlow [MVP - Outlook]

unread,
Sep 17, 2003, 2:17:45 PM9/17/03
to
OHM,
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
Dim dialog as New OptionsDialog
dialog.ShowDialog()
dialog.Dispose()
End Sub

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

End Sub

The problem is you cannot edit the menu from the Component Designer. You
can use cut & paste from a form to get the menu to the component...

Hope this helps
Jay

"One Handed Man [ OHM ]" <terry_burnsREMOVE%FOR%NO%SP...@BTOpenworld.com>

wrote in message news:e%23bszDUf...@TK2MSFTNGP12.phx.gbl...

CJ Taylor

unread,
Sep 17, 2003, 2:38:12 PM9/17/03
to

"Armin Zingler" <az.n...@freenet.de> wrote in message
news:OT32AWUf...@TK2MSFTNGP12.phx.gbl...

> "One Handed Man [ OHM ]"
> <terry_burnsREMOVE%FOR%NO%SP...@BTOpenworld.com> schrieb
> > If this is duplicated, I'm sorry because I cant find the original
> > post I made today
> >
> > Anyway . .
> >
> > Now its my turn to ask a question
> >
> > I want to develop an app which will run in the system tray. How can I
> > do this?
>
> There is not much space in the system tray, so your app might get hurt.
;-))
>
>

*sigh*... I know you didn't just say that....

One Handed Man [ OHM ]

unread,
Sep 18, 2003, 3:36:14 AM9/18/03
to
Excellent - Thank You

"Jay B. Harlow [MVP - Outlook]" <Jay_H...@email.msn.com> wrote in message
news:%23tsRCgU...@TK2MSFTNGP11.phx.gbl...

One Handed Man [ OHM ]

unread,
Sep 18, 2003, 9:21:31 AM9/18/03
to
I couldnt quite get your code to work, so this is what I ended up with.

Public Sub New()

MyBase.New()

'This call is required by the Component Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

menuOptions.MenuItems.Clear()

menuOptions.MenuItems.Add("Exit", AddressOf HandleNIExit)

'Following two items need handlers written for them, but they goto the exit
handler for now

menuOptions.MenuItems.Add("Option1", AddressOf HandleNIExit)

menuOptions.MenuItems.Add("Option2", AddressOf HandleNIExit)

NotifyIcon1.ContextMenu = menuOptions

End Sub

AND

Sub HandleNIExit(ByVal sender As Object, ByVal e As System.EventArgs)

Application.Exit()

End Sub

"Jay B. Harlow [MVP - Outlook]" <Jay_H...@email.msn.com> wrote in message
news:%23tsRCgU...@TK2MSFTNGP11.phx.gbl...

One Handed Man [ OHM ]

unread,
Sep 18, 2003, 9:23:51 AM9/18/03
to

The icon takes about 30 seconds to disapear from the tray eafter exit. Is
this expected ?

"One Handed Man [ OHM ]" <terry_burnsREMOVE%FOR%NO%SP...@BTOpenworld.com>

wrote in message news:uNSJrcbf...@tk2msftngp13.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Sep 18, 2003, 10:13:35 AM9/18/03
to
OHM,
I used cut & paste to get the menu on the component.

Remember that the menu variables need to have WithEvents on them to support
the Handles clause on the Sub.

As you found out, there are at least 3 methods of doing things in .NET,
manually creating the menus is acceptable.

About the 3 seconds, I cannot find my sample. I want to say you need to
Dispose of the component to get the icon to disappear quicker.

If I find it later I will post the info.

Hope this helps
Jay

"One Handed Man [ OHM ]" <terry_burnsREMOVE%FOR%NO%SP...@BTOpenworld.com>

wrote in message news:eFgXndef...@TK2MSFTNGP09.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Sep 18, 2003, 10:46:36 AM9/18/03
to
OHM,
I found my sample. You need to call the Dispose method of the Component
before you call Application.Exit.

In VS.NET this causes the icon to disappear 'immediately'.

FWI: you should use the NotifyIcon.MouseDown & NotifyIcon.MouseUp events if
you want to track which mouse button was pressed instead of the
NotifyIcon.Click event. I had 'trouble' with this when I first worked with
my sample. Took me 'for ever' to figure out what to do ;-)

Hope this helps
Jay

"One Handed Man [ OHM ]" <terry_burnsREMOVE%FOR%NO%SP...@BTOpenworld.com>

wrote in message news:eCht6eef...@TK2MSFTNGP12.phx.gbl...

One Handed Man [ OHM ]

unread,
Sep 18, 2003, 11:55:51 AM9/18/03
to
Thanks for your reply. I have two more questions.

1.) I need to call dispose before application exit
Right, as you suggest, the component object is removed immediatly when
disposed. What I would like to know is, if you do not do this first I guess
the application looks around for a while to see if it has any objects
associated with it before closing down ?

2.) In my variation of the code I simply added delegate handlers using the
constructor for the menuitem. I did not specifically add them withevents.
does this mean that this method does this implicitly, or have I dont
something wrong ?


Regards OHM

"Jay B. Harlow [MVP - Outlook]" <Jay_H...@email.msn.com> wrote in message

news:uGg0y9ef...@TK2MSFTNGP11.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Sep 18, 2003, 12:05:13 PM9/18/03
to
OHM,

> 1.) I need to call dispose before application exit
Actually the program exits, about 3 seconds later Windows notices that the
program for the icon is not running any more and removes the icon. You can
see the same effect if you start the program in VS.NET. Then use the VS.NET
'Debug - Stop Debugging' command. Also if you move the mouse over the icon
the icon will disappear in this case.

> 2.) In my variation of the code I simply added delegate handlers using the

The MenuItem constructor that accepts the delegate is calling AddHandler
inside. So yes the constructor is doing it implicitly. It is valid and is
easier when creating menus without the aid of the designer.

Hope this helps
Jay


"One Handed Man [ OHM ]" <terry_burnsREMOVE%FOR%NO%SP...@BTOpenworld.com>

wrote in message news:Oc$22zffD...@tk2msftngp13.phx.gbl...

One Handed Man [ OHM ]

unread,
Sep 18, 2003, 12:10:53 PM9/18/03
to
Many thanks for your help, I'm off and running now.


Regards - OHM

"Jay B. Harlow [MVP - Outlook]" <Jay_H...@email.msn.com> wrote in message

news:eOLpo6ff...@TK2MSFTNGP09.phx.gbl...

0 new messages