NotifyIcon onClick事件未触发

时间:2015-07-14 13:45:15

标签: vb.net delegates notifyicon begininvoke invoke-command

有人能告诉我为什么这段代码中的onclick事件不起作用? 其他一切都有效。只是onclick事件不是! 另外,我如何通过fileName以便它可以像这样使用:

   BalloonTipText=FileName

代码:

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

2 个答案:

答案 0 :(得分:0)

  

如何传递fileName,以便像这样使用: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

这也经过了尝试和测试。我不确定你是否将控件拖放到表单上。在我的示例中,我创建了一个新变量Public WithEvents NotifyIcon1...,用于创建NotifyIcon

修改

我注意到您使用的签名错误...

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

应该是......

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

看第一行。您有MouseEventArgs它应该是System.EventArgs,而您的句柄不应该是MouseClick而是NotifyIcon1.Click

答案 1 :(得分:0)

这样的解决方案怎么样:

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

然后在您的表格或任何地方:

' 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