|
-
Nov 15th, 2021, 03:03 PM
#1
Thread Starter
Fanatic Member
[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
-
Nov 15th, 2021, 03:26 PM
#2
Re: Select Case Control
You only have two options, so Select Case is perhaps a bit of a reach. Still, did you go out on a limb and try:
Code:
Select Case Sender
Case Is ComboBox1
End Select
My usual boring signature: Nothing
 
-
Nov 15th, 2021, 03:49 PM
#3
Thread Starter
Fanatic Member
Re: Select Case Control
I get a "Rational operator expected" error on Case Is ComboBox1
-
Nov 15th, 2021, 04:00 PM
#4
Re: Select Case Control
I would setup the select/case to compare the control's name:
Code:
Select Case sender.Name
Case ComboBox1.Name
' etc...
End Select
-
Nov 15th, 2021, 04:14 PM
#5
Thread Starter
Fanatic Member
Re: Select Case Control
That's it. Thank you so much.
Intellisense doesn't bring up .Name on either of those.
-
Nov 15th, 2021, 04:37 PM
#6
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
' ...
-
Nov 15th, 2021, 05:02 PM
#7
Thread Starter
Fanatic Member
Re: [RESOLVED] Select Case Control
Hard to explain but a picture might help. I can't implement what you suggested.
Attachment 183016
-
Nov 15th, 2021, 05:05 PM
#8
Thread Starter
Fanatic Member
Re: [RESOLVED] Select Case Control
-
Nov 15th, 2021, 06:02 PM
#9
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.
My usual boring signature: Nothing
 
-
Nov 16th, 2021, 12:56 AM
#10
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.
-
Nov 16th, 2021, 12:06 PM
#11
Re: [RESOLVED] Select Case Control
That's a good point. I overlooked that.
My usual boring signature: Nothing
 
-
Nov 16th, 2021, 12:49 PM
#12
Re: [RESOLVED] Select Case Control
I too couldn't see the forest from the trees.
-
Nov 16th, 2021, 06:38 PM
#13
Re: [RESOLVED] Select Case Control
 Originally Posted by Shaggy Hiker
That's a good point. I overlooked that.
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|