2

我有几个标签都是这样编码的:

Public assessment_menu_button As New Label
Public current_label_clicked As New Label

AddHandler assessment_menu_button.Click, AddressOf click_assessment_menu_button

Private Sub click_assessment_menu_button(ByVal sender As System.Object, 
                                         ByVal e As System.EventArgs)
   current_label_clicked = sender
   ' do some other stuff
End Sub

然后,稍后在我的程序中,我有一个 Sub 需要对放入 current_label_clicked 的任何标签执行点击并在其上引发点击事件。就像是

Private Sub whatever()
    current_label_clicked.performClick()
End Sub

但你不能用标签做到这一点。

那么如何引发标签的点击事件呢?

谢谢。

4

2 回答 2

4

假设您的标签名为 Label1。

这个 Sub 就是点击标签时会执行的 Sub。

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

End Sub

要引发标签点击,您只需调用该事件。

Label1_Click(Label1, Nothing)

就是这样 :)

于 2013-08-27T14:03:52.337 回答
1

直接调用事件处理程序方法被认为是不好的形式。将单击标签时需要调用的代码放入方法中,并从标签单击处理程序和任何方法中调用该方法:

Private Sub click_assessment_menu_button(ByVal sender As System.Object, ByVal e As System.EventArgs)
    runLabelCode(sender)

    'other code here
End Sub

Private Sub runLabelCode(sender As Label)
    current_label_clicked = sender

    'other code here
End Sub

'elsewhere in the code
Private Sub Whatever()
    runLabelCode(Label1, Nothing)
End Sub
于 2013-08-27T14:29:19.747 回答