在vb.net中如何为标签引发click事件

时间:2013-08-27 13:57:16

标签: vb.net click raiseevent

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

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

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

那么如何提升标签的点击事件呢?

感谢。

2 个答案:

答案 0 :(得分:4)

假设您的标签名为Label1。

此Sub是单击标签时将执行的Sub。

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

End Sub

要提出标签点击,您只需要调用该事件。

Label1_Click(Label1, Nothing)

就是这样:))

答案 1 :(得分: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