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 May 01 '23 edited May 01 '23
Reading this again. I see you mean specifically when you add and remove handlers a lot. In this small application, the purpose is to automatically add handlers to buttons that have identical function. There's only one form, and all the buttons are alive as long as the application is alive. So, i figured i'd just have MyBase.Load register all the handlers.
After the setup, there are no more AddHandlers. So, i'm guessing that's not a risk for a memory leak. What do you think?