这是一个快速示例,显示了复选框上单击事件的执行顺序:
Private Sub CheckBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.MouseDown
Dim a As Integer = 1
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Dim a As Integer = 2
End Sub
Private Sub CheckBox1_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.Click
Dim a As Integer = 3
End Sub
Private Sub CheckBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.MouseClick
Dim a As Integer = 4
End Sub
Private Sub CheckBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.MouseUp
Dim a As Integer = 5
End Sub
如果您在每个声明上设置断点,您会注意到在 CheckChanged 之前只触发了 MouseDown 事件。这意味着如果您想查看用户是否单击了复选框,则必须在 CheckBox.MouseDown 子中触发一个事件。请注意,任何时候用户单击此复选框都会触发此事件,即使他们在按住鼠标的同时将鼠标拖出它也不会更新检查更改事件。这只是意味着您必须在 MouseUp 子中触发后续事件以清除标志。
处理它的一种方法是这样的:
Private blIsUserClick As Boolean
Private Sub CheckBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.MouseDown
blIsUserClick = True
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If blIsUserClick Then
'Is a user click event
Else
'Not a user click event
End If
End Sub
Private Sub CheckBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.MouseUp
blIsUserClick = False
End Sub