[RESOLVED] Select Case Control
Is it possible to Select Case by the control
HTML Code:
Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged
'This works
If sender Is ComboBox1 Then
End If
'This doesn't
Select Case sender
Case ComboBox1
End Select
'And neither does this
Dim myComboBox = DirectCast(sender, System.Windows.Forms.ComboBox)
Select Case myComboBox
Case ComboBox1
End Select
End Sub
Re: [RESOLVED] Select Case Control
Sender at that point would still be object, you should explicitly cast it to a control:
Code:
Select Case DirectCast(sender, Control).Name
' ...
Re: [RESOLVED] Select Case Control
Hard to explain but a picture might help. I can't implement what you suggested.
Attachment 183016
Re: [RESOLVED] Select Case Control
Re: [RESOLVED] Select Case Control
Yeah, attachments have issues, as you can see.
Sender is a control, so you can cast to that. Still, I like my answer better. If you look at the answers in this SO thread:
https://stackoverflow.com/questions/...type-in-vb-net
You'll find that the question is not quite the same as what you are asking for, as they are trying to switch on the type, which wouldn't work for you at all, since the types are the same. However, you will also find an example of what I suggested as the third answer, and that should work for you.
Re: [RESOLVED] Select Case Control
This question is just wrong from the outset. The whole point of using a common event handler is to do the same thing for multiple objects. If you are trying to differentiate between the different objects that might raise the event then you shouldn't be using a common event handler in the first place. If you want to do one thing for ComboBox1 and something else for ComboBox2 then use two different methods to handle the event for the two different ComboBoxes. If you want to do partly the same thing and partly something different then put the part that's the same in a method and then call that method from both event handlers.
Re: [RESOLVED] Select Case Control
That's a good point. I overlooked that.
Re: [RESOLVED] Select Case Control
I too couldn't see the forest from the trees.
Re: [RESOLVED] Select Case Control
Quote:
Originally Posted by
Shaggy Hiker
That's a good point. I overlooked that.
Quote:
Originally Posted by
dday9
I too couldn't see the forest from the trees.
There may well be a situation where the question is valid, so knowing the answer is not a bad thing. This just isn't that situation.