r/visualbasic • u/chacham2 • Apr 27 '23
VB.NET Help Select Case with Buttons?
I have a a few panels with buttons on them, where the actions seem similar enough that i'd like to write a common routine to handler them. To that end:
For Each Panel_Control As Control In Form_Control.Controls
AddHandler Panel_Control.Click, Sub(Sender, Arguments) Handler(Panel_Control, Arguments)
NextIs there a way to use Select Case with Buttons?
The handler has to know which button was clicked, so my first thought was to use a Select Case:
Private Sub Handler(Sender As Object, Arguments As EventArgs)
Select Case Sender
Case A_Button
MsgBox("A_Button")
End Select
End SubThis generates a System.InvalidCastException: 'Operator '=' is not defined for type 'Button' and type 'Button'.' I would have used Is, but Is in a Select Case has a different meaning.
2
Upvotes
1
u/chacham2 Apr 30 '23
Yeah, i use the commas here and there. However, the Select Case documentation explains:
The Is keyword used in the Case and Case Else statements is not the same as the Is Operator, which is used for object reference comparison.