首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NotifyIcon onClick事件未触发

NotifyIcon onClick事件未触发
EN

Stack Overflow用户
提问于 2015-07-14 21:45:15
回答 2查看 1K关注 0票数 0

有人能告诉我为什么这段代码中的onclick事件不工作吗?其他的都没问题。只有onclick事件不是!另外,我如何传递fileName才能像这样使用它:

代码语言:javascript
复制
   BalloonTipText=FileName

代码:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2015-07-14 22:37:16

如何传递fileName才能像这样使用:BalloonTipText=FileName

代码语言:javascript
复制
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

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

编辑

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

代码语言:javascript
复制
Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
 MessageBox.Show("Text clicked")
 'This is not working!!!
End Sub

应该是..。

代码语言:javascript
复制
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

票数 0
EN

Stack Overflow用户

发布于 2015-07-15 05:01:38

这样的解决方案如何:

代码语言:javascript
复制
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

然后在您的表单中或任何位置:

代码语言:javascript
复制
' 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31408475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档