Vb.net NotifyIcon onClick事件未触发
有人能告诉我为什么代码中的onclick事件不起作用吗? 其他一切都可以。只是onclick事件不是! 另外,我如何传递Vb.net NotifyIcon onClick事件未触发,vb.net,delegates,notifyicon,begininvoke,invoke-command,Vb.net,Delegates,Notifyicon,Begininvoke,Invoke Command,有人能告诉我为什么代码中的onclick事件不起作用吗? 其他一切都可以。只是onclick事件不是! 另外,我如何传递文件名,以便可以像这样使用它: BalloonTipText=FileName 代码: 我如何传递文件名以便可以这样使用:balloodtiptext=fileName 这也经过了尝试和测试。我不确定是否将控件拖放到窗体上。在我的示例中,我创建了一个新变量Public with events NotifyIcon1…,该变量创建了NotifyIcon 编辑 我注意到你使
文件名
,以便可以像这样使用它:
BalloonTipText=FileName
代码:
我如何传递文件名以便可以这样使用:balloodtiptext=fileName
这也经过了尝试和测试。我不确定是否将控件拖放到窗体上。在我的示例中,我创建了一个新变量Public with events 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。改为单击。这样的解决方案如何:
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
尝试在Handle\u onDocument中使用AddHandler成功设置NotifyIcon1的OnClick事件处理程序。@NZ03请查看我的编辑,看看是否有帮助。非常感谢。我在一个小的winform应用程序中尝试了你的代码,效果很好。但是,它在我的应用程序中不起作用:-(我想我在用线程做一些错误的事情!@NZ03不确定。请发布所有相关代码
,否则很难知道你可能做错了什么。代码使用delgates的方式是:myreq.SuccessAction=(AddressOfOnDocumentSuccessed)myreq.FailAction=(AddressOf OnDocumentFailure)正在调用dll以在不同线程上生成文档。生成文档时,应用程序应该通过显示BallootTip消息来通知用户。当用户单击该消息时,包含该消息的文件夹将被打开。除onclick事件外,一切正常!
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