System Tray Applications

  • Thread starter Thread starter One Handed Man [ OHM ]
  • Start date Start date
O

One Handed Man [ OHM ]

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
 
"One Handed Man [ OHM ]"
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. ;-))


Use the NotifyIcon component (System.Windows.Forms.NotifyIcon)
 
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
 
Armin Zingler said:
"One Handed Man [ OHM ]"
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....
 
Excellent - Thank You



Jay B. Harlow said:
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
 
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
 
The icon takes about 30 seconds to disapear from the tray eafter exit. Is
this expected ?
 
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 said:
The icon takes about 30 seconds to disapear from the tray eafter exit. Is
this expected ?



One Handed Man said:
Excellent - Thank You
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 ]"
wrote in message If this is duplicated, I'm sorry because I cant find the original
post
I
 
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 said:
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 said:
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
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 ]"
wrote in message If this is duplicated, I'm sorry because I cant find the original
post
I
 
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 said:
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





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 said:
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



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 ]"
wrote in message 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
 
Many thanks for your help, I'm off and running now.


Regards - OHM



Jay B. Harlow said:
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 said:
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





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 ]"
wrote in message 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



message
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 ]"
wrote in message 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
 
Back
Top