如果单击鼠标,则不执行鼠标离开事件

时间:2018-03-22 09:12:10

标签: vb.net

您好我想显示一个Label提示,所以如果鼠标悬停然后显示Label并且鼠标离开则隐藏Label。 但是如果鼠标点击然后显示标签并且不执行离开事件,因为离开事件意味着隐藏鼠标。那我该怎么办呢?我的代码在这里。

点击活动

Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
    control("set")
End Sub

悬停事件

Private Sub Label2_MouseHover(sender As Object, e As EventArgs) Handles Label2.MouseHover
    control("show")
End Sub

离开活动

Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles Label2.MouseLeave
    control("remove")
End Sub

控制子

Public Sub control(ByVal c As String)
    If c = "set" Then
        Label3.Visible = True
    ElseIf c = "show" Then
        Label3.Visible = True
    ElseIf c = "remove" Then
        Label3.Visible = False
    End If 
End Sub

1 个答案:

答案 0 :(得分:0)

点击EventHandler后,您可以移除Label2

Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
    RemoveHandler Label2.MouseLeave, AddressOf Label2_MouseLeave
End Sub

不确定control - 方法的目的是什么exaclty ......但代码可以简化为:

Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
    RemoveHandler Label2.MouseLeave, AddressOf Label2_MouseLeave
End Sub

Private Sub Label2_MouseHover(sender As Object, e As EventArgs) Handles Label2.MouseHover
    Label3.Visible = True
End Sub

Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles Label2.MouseLeave
    Label3.Visible = False
End Sub