Dynamically add eventhandler for notifyIcon

G

Guest

I am using VB Net 2005 and want to use the notifyicon. I do not have a
form, only a class. I have no problems showing or modifying the
NotifyIcon, however, I am having a problem handling the click event. I
have tried unsuccessfully using AddHandler:

AddHandler nIcon.Click AddressOf nIcon_Click

Private Sub nIcon_Click(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("Click")
End Sub

Any help would be greatly appreciated.
 
S

Smokey Grindle

where is your add handler at? in the load? does it depend on another event
happening first? compare it to the sample code from MSDN

' Initalize the NofifyIcon object's shortcut menu.
Private Sub InitializeContextMenu()
Dim menuList() As MenuItem = New MenuItem() _
{New MenuItem("Sign In"), New MenuItem("Get Help"), _
New MenuItem("Open")}
Dim clickMenu As New ContextMenu(menuList)
NotifyIcon1.ContextMenu = clickMenu
End Sub


' When user clicks the left mouse button display the shortcut menu.
' Use the SystemInformation.PrimaryMonitorMaximizedWindowSize property
' to place the menu at the lower corner of the screen.
Private Sub NotifyIcon1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles NotifyIcon1.Click

Dim windowSize As System.Drawing.Size = _
SystemInformation.PrimaryMonitorMaximizedWindowSize
Dim menuPoint As System.Drawing.Point = New System.Drawing.Point _
(windowSize.Width - 180, windowSize.Height - 5)
menuPoint = Me.PointToClient(menuPoint)

NotifyIcon1.ContextMenu.Show(Me, menuPoint)
End Sub
 
G

Guest

Smokey Grindle wrote:
where is your add handler at? in the load?
Yes:
Public Sub New()
AddHandler nIcon.Click, AddressOf nIcon_Click
updateInt =
Registry.LocalMachine.OpenSubKey("Software\SI_Client").GetValue("Update_int")
State()
End Sub


does it depend on another event happening first?
No. I would like the event to be raised at any point when the icon is
clicked. I am wondering if the State Sub has anything to do with the
handling. The program reads an update interval from the registry, and
then continuously processes and sleeps. Could it be that this is
stopping the event from being raised?

Public Sub State()
updateInt =
Registry.LocalMachine.OpenSubKey("Software\SI_Client").GetValue("Update_int")
Dim bln As Boolean = True '(Always true for testing purposes
only...)
Do While bln
status =
Registry.LocalMachine.OpenSubKey("Software\SI_Client").GetValue("color")

If status = "green" Then
SetGreen()
Else
SetRed()
End If

Thread.Sleep(updateInt)
Loop
End Sub
 
S

Smokey Grindle

I think you are putting your entire application to thread sleep with that
thread.sleep there, you should do multi threading and put your "state" sub
in a seperate thread, so when paused it wont affect your UI thread, which I
think it is doing
 
G

Guest

Guess I just needed to talk it out... Thanks so much!


Smokey Grindle wrote:
I think you are putting your entire application to thread sleep with
that
thread.sleep there, you should do multi threading and put your "state"
sub
in a seperate thread, so when paused it wont affect your UI thread,
which I
think it is doing
 

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

Similar Threads


Top