I’m working on a function that handles events from a number of buttons and trying to define a Select…Case based on the clicked button. Something like this…
Private Sub btnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnThis.Click, _
btnThat.Click, _
btnTheOther.Click
Dim button_clicked As Button = CType(sender, Button)
...Do some common parts...
Select button_clicked
Case btnThis
...Do this...
Case btnThat
...Do that...
Case btnTheOther
...Do the other...
Case Else
End Select
But I can’t get it to compile; it reports that “Operator = is not defined for types ‘System.Windows.Forms.Button’ and ‘System.Windows.Forms.Button'”. I’ve also tried
Select button_clicked
Case btnThis.Equals
and
Select button_clicked
Case btnThis.Equals(button_clicked)
Which don’t work either. Is there a way I can define a Select…Case in this manner or is it always destined to fail due to the attempt at comparison of reference types?
Using the control’s name is one option: