【问题标题】:NotifyIcon onClick event not firingNotifyIcon onClick 事件未触发
【发布时间】:2015-07-14 13:45:14
【问题描述】:

谁能告诉我为什么这段代码中的 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

【问题讨论】:

  • 尝试使用 Handle_OnDocumentSucceeded 中的 AddHandler 设置 NotifyIcon1 的 OnClick 事件处理程序。
  • @NZ03 请查看我的编辑,看看是否有帮助。

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


【解决方案1】:

这样的解决方案怎么样:

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

【讨论】:

    【解决方案2】:

    我怎样才能传递文件名以便它可以像这样使用: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

    【讨论】:

    • 非常感谢。我在一个小的winform应用程序中尝试了你的代码,它工作正常。但是,它在我的应用程序中不起作用:-(我认为我在线程上做错了!
    • @NZ03 不确定。 Please post all relevant code 否则真的很难知道你做错了什么。
    • 代码以这种方式使用 delgates: myreq.SuccessAction = (AddressOf OnDocumentSucceeded) myreq.FailAction = (AddressOf OnDocumentFailure) 正在调用 dll 以在不同线程上生成文档。生成文档时,应用程序应该通过显示气球提示消息来通知用户。当用户单击该消息时,将打开包含的文件夹。一切正常,期待 onclick 事件!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 2013-04-30
    • 2011-02-05
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多