在 vb.net 中如何为标签引发点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18467519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
in vb.net how do I raise a click event for a label
提问by John
I have a several labels all coded as such:
我有几个标签都这样编码:
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
Then, later in my program, I have a Sub which needs to perform a click on whichever label was put into current_label_clicked and raise a click event on it. Something like
然后,稍后在我的程序中,我有一个 Sub 需要对放入 current_label_clicked 的任何标签执行单击并在其上引发单击事件。就像是
Private Sub whatever()
current_label_clicked.performClick()
End Sub
but you can't do that with labels.
但你不能用标签来做到这一点。
So how do I raise the click event for the label?
那么如何引发标签的点击事件呢?
Thanks.
谢谢。
回答by William Gérald Blondel
Let's say your label is named Label1.
假设您的标签名为 Label1。
This Sub is the Sub that will be executed when you click on the label.
这个 Sub 是当你点击标签时将执行的 Sub。
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
To raise a label click, you just need to call that event.
要引发标签点击,您只需调用该事件。
Label1_Click(Label1, Nothing)
That's it :)
就是这样 :)
回答by Chris Dunaway
It's considered bad form to call the event handler method directly. Put the code you need to call when the label is clicked into a method and call that method from both the label click handler and the whatever method:
直接调用事件处理程序方法被认为是不好的形式。将单击标签时需要调用的代码放入一个方法中,然后从标签单击处理程序和任何方法调用该方法:
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

