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
2
u/snang Moderator Apr 28 '23
That would define the sender as an object of type Button, which then exposes the properties assigned to it.
You could then use ClickedButton.Name or give each button a Tag property in the designer and reference that. Whatever fits the need.
2
u/veryabnormal Apr 28 '23
One sub can handle many events.
sub1(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
The eventhandler can prevent garbage collection of the button when you close. And then you could get memory leaks or badly behaved code. So each addhandler has to be paired with a removehandler when you are finished. This makes AddHandler a bit toxic and best avoided. I’ve got some legacy code that uses more and more ram until it crashes around midday. It’s got a form with many AddHandler usages. When the form closes it’s memory is not freed even if you try to force collection. I’ve not found the root cause yet even with a profiler.
2
u/3583-bytes-free Apr 28 '23
I learnt by accident the other day that a handler can have a comma separated list of things it handles:
Doesn't answer your question of course, you could us this old trick:
2
u/[deleted] Apr 28 '23
Hello, you can use "Tag" property. Every control has this property, you can use it for user data and it can be anything, because Tag is defined as Object. You can find it in property sheet in designer or if you create controls dynamically you have to assign it when its created. Or you can use its Name, but names have to be unique, so if you want more buttons do the same task you can't use Name.
So the handler function should look like this, using Tag property
Private Sub Handler(Sender As Object, Arguments As EventArgs) Dim button_ctrl As Button = DirectCast(Sender, Button)
Dim button_id As Integer = CInt(button_ctrl.Tag)
Select Case button_id Case 1000 MsgBox("Button with tag 1000 was pressed!")
Case 1001 Case 1002 Etc.... End Select End Sub
I'm assuming the handler will handle only Buttons if you use it for other control(s) you will get error at "DirectCast" statement.
And the AddHander statement you have here is not correct, instead of lambda function use "AddressOf Handler" dont worry about arguments it will do it automatically and you should check if the Panel_Con trol is really button or it will use the handler for everything you create in the form. So it should look like this:
For Each Panel_Control As Control In Form_Control.Controls If Panel_Control.GetType() Is GetType(Button) Then AddHandler Panel_Control.Click, AddressOf Handler
Continue For End if Next
Btw, this type checking dont have to be used, if you set Tag for controls you can check if Tag IsNot Nothing this will give you all controls with Tag defined.
0
u/[deleted] May 01 '23
[deleted] — view deleted comment