in vb.net how do I raise a click event for a label

12.3k views Asked by At

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

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.

2

There are 2 answers

5
William Gérald Blondel On BEST ANSWER

Let's say your label is named Label1.

This Sub is the Sub that will be executed when you click on the label.

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 :)

0
Chris Dunaway On

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