Results 1 to 13 of 13

Thread: [RESOLVED] Select Case Control

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Resolved [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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: Select Case Control

    I get a "Rational operator expected" error on Case Is ComboBox1

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: Select Case Control

    That's it. Thank you so much.
    Intellisense doesn't bring up .Name on either of those.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    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
        ' ...
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: [RESOLVED] Select Case Control

    Hard to explain but a picture might help. I can't implement what you suggested.
    Attachment 183016

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: [RESOLVED] Select Case Control


  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    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

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] Select Case Control

    That's a good point. I overlooked that.
    My usual boring signature: Nothing

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: [RESOLVED] Select Case Control

    I too couldn't see the forest from the trees.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] Select Case Control

    Quote Originally Posted by Shaggy Hiker View Post
    That's a good point. I overlooked that.
    Quote Originally Posted by dday9 View Post
    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
  •  



Click Here to Expand Forum to Full Width