0

Could anyone tell me why the onclick event in this code isn't working? Everything else works. Just the onclick event isn't! Also, how can I pass the fileName so it can be used like this:

   BalloonTipText=FileName

Code:

Delegate Sub InvokeDelegate()

  Public Sub OnDocumentSucceeded(fileName As String)
    If Not Me.IsHandleCreated Then
      Me.CreateHandle()
    End If
    Invoke(New InvokeDelegate(AddressOf Handle_OnDocumentSucceeded))
  End Sub

Public Sub Handle_OnDocumentSucceeded()
  NotifyIcon1.Icon = SystemIcons.Exclamation
  NotifyIcon1.BalloonTipTitle = "Your document has been generated"
  'NotifyIcon1.BalloonTipText = fileName
  NotifyIcon1.BalloonTipText = "testing...."
  NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
  NotifyIcon1.Visible = True
  NotifyIcon1.ShowBalloonTip(5000)      
End Sub

Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
     MessageBox.Show("Text clicked")
     'This is not working!!!
End Sub
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
NZ03
  • 1
  • 2

2 Answers2

0

How can I pass the fileName so it can be used like this: BalloonTipText=FileName

Public Delegate Sub InvokeDelegate(ByVal strFileName As String) 'Create delegate where you can pass the file name in.

Public WithEvents NotifyIcon1 As New NotifyIcon 'I didn't drop it on my form... Also if you do this you wont have to handle any handlers.

Public Sub OnDocumentSucceeded(fileName As String)
    If Not Me.IsHandleCreated Then
        Me.CreateHandle()
    End If
    Invoke(New InvokeDelegate(AddressOf Handle_OnDocumentSucceeded), fileName)
End Sub

Public Sub Handle_OnDocumentSucceeded(ByVal strName As String)
    NotifyIcon1.Icon = SystemIcons.Exclamation
    NotifyIcon1.BalloonTipTitle = "Your document has been generated"
    NotifyIcon1.BalloonTipText = strName
    NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
    NotifyIcon1.Visible = True
    NotifyIcon1.ShowBalloonTip(5000)
End Sub

Private Sub NotifyIcon1_BalloonTipClicked(sender As Object, e As System.EventArgs) Handles NotifyIcon1.BalloonTipClicked
    MessageBox.Show("Text clicked")
End Sub

Private Sub NotifyIcon1_Click(sender As Object, e As System.EventArgs) Handles NotifyIcon1.Click
    MessageBox.Show("Notify clicked")
End Sub

This has been tried and tested as well. I am not sure if you dragged and dropped the control onto the form or not. In my example I created a new variable Public WithEvents NotifyIcon1... that creates the NotifyIcon.

Edit

I noticed the signature is wrong you are using...

Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
 MessageBox.Show("Text clicked")
 'This is not working!!!
End Sub

It should be...

Private Sub NotifyIcon1_Click(sender As Object, e As System.EventArgs) Handles NotifyIcon1.Click
  MessageBox.Show("Notify clicked")
End Sub

Look at the first line. You have MouseEventArgs it should be System.EventArgs and your handles shouldnt be MouseClick but NotifyIcon1.Click instead.

Trevor
  • 7,777
  • 6
  • 31
  • 50
  • Thanks very much for that. I tried your code in a small winform appliation and it works fine. However, it doesn't work in my application :-( I think I'm doing something wrrong with threads! – NZ03 Jul 14 '15 at 15:39
  • @NZ03 not sure then. `Please post all relevant code` otherwise it's really hard to know what you may be doing wrong. – Trevor Jul 14 '15 at 16:01
  • The code uses delgates this way: myreq.SuccessAction = (AddressOf OnDocumentSucceeded) myreq.FailAction = (AddressOf OnDocumentFailure) A dll is being called to generate documents on different threads. When a document is generated, application is supposed to notify user by displaying balloontip message. When user clicks on that message, the containing folder is opened. Everything works fine, expect the onclick event! – NZ03 Jul 14 '15 at 19:02
0

How about a solution like this:

Public Class BalloonNotifier

    Public Shared Sub ShowInfoBalloon(ByVal title As String, ByVal text As String)
        Dim ni As NotifyIcon = CreateNotification()
        ni.Icon = SystemIcons.Exclamation
        ShowBalloon(ni, title, text)
    End Sub

    Public Shared Sub ShowFailBalloon(ByVal title As String, ByVal text As String)
        Dim ni As NotifyIcon = CreateNotification()
        ni.Icon = SystemIcons.Error
        ShowBalloon(ni, title, text)
    End Sub

    ' helper to create a new NotifyIcon
    Private Shared Function CreateNotification() As NotifyIcon
        Dim notifyIcon As NotifyIcon = New NotifyIcon()

        notifyIcon.Visible = True

        ' assuming you want to handle both the balloon being clicked and the icon that remains in the tray.
        AddHandler notifyIcon.BalloonTipClicked, AddressOf BalloonTipClicked
        AddHandler notifyIcon.Click, AddressOf BalloonTipClicked

        Return notifyIcon
    End Function

    Private Shared Sub BalloonTipClicked(sender As Object, e As EventArgs)
        Dim notifyIcon As NotifyIcon = sender
        MessageBox.Show(String.Format("Clicked on Notifier for document ""{0}""", notifyIcon.BalloonTipText))

        ' lets hide the balloon and its icon after click
        notifyIcon.Visible = False
        notifyIcon.BalloonTipIcon = Nothing
        notifyIcon.Dispose()
    End Sub

    Private Shared Sub ShowBalloon(ByRef notifyicon As NotifyIcon, ByVal title As String, ByVal text As String)
        notifyicon.Visible = True
        notifyicon.BalloonTipText = text
        notifyicon.BalloonTipTitle = title
        notifyicon.ShowBalloonTip(5000)
    End Sub

End Class

Then in your Form or wherever:

' delegates with parameters
    Delegate Sub OnDocumentSucceeded(ByVal notification As String, ByVal filename As String)
    Delegate Sub OnDocumentFailed(ByVal notification As String, ByVal filename As String)

    Public Sub DocumentSucceeded(ByVal filename As String)
        ' notify success
        Invoke(New OnDocumentSucceeded(AddressOf BalloonNotifier.ShowInfoBalloon), "Your file was created!", filename)
    End Sub

    Public Sub DocumentFailed(ByVal filename As String)
        ' notify fail
        Invoke(New OnDocumentSucceeded(AddressOf BalloonNotifier.ShowFailBalloon), "Creating document failed!", filename)
    End Sub
Stokke
  • 1,871
  • 2
  • 13
  • 18