Using sender as a case in Select Case Statement

When my form loads it adds some handles (using AddHandler) to a sub (showStatus). What this sub does is checks which control activated the sub (using sender.Equals) and displays text in the status label (status) accordingly.

For this I use an If...Then statement for each possibility. There are many possibilities and my code get cluttered. Is there a way to do the same thing with a Select...Case statement. I tried:

Select Case sender
Case tbOne : status.Text = "blahblahblahblahblahblah"
End Select

where: tbOne is a textbox, status is a StatusLabel

It gives me an error though, saying "Operator '=' is not defined for types 'Object' and 'System.Windows.Forms.TextBox'."

Any ideas on how I can get this to work with a select case statement




Answer this question

Using sender as a case in Select Case Statement

  • Megakemp

    You can use Select Case as in the following:

    Select Case True
    Case sender Is TextBox1
    Label1.Text = TextBox1.Text
    Case sender Is TextBox2
    Label1.Text = TextBox2.Text
    End Select

    However, this translates into IL (and decompiled using Reflector) as:

    Dim flag1 As Boolean = True
    If (flag1 = (sender Is Me.TextBox1)) Then
    Me.Label1.Text = Me.TextBox1.Text
    Else
    If (flag1 = (sender Is Me.TextBox2)) Then
    Me.Label1.Text = Me.TextBox2.Text
    End If
    End If

    I don't know if this helps.



  • hssj

    Public Class Form1

         Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

               Handles Button1.Click

               whereFrom(sender.Name)

         End Sub

         Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

              Handles Button2.Click

              whereFrom(sender.Name)

         End Sub

         Private Sub whereFrom(ByVal senderName)

              Select Case senderName

                   Case "Button1"

                        Button1.Text = "sender"

                        Button2.Text = ""

                   Case "Button2"

                        Button2.Text = "sender"

                        Button1.Text = ""

              End Select

         End Sub

    End Class

    ... or you can just pass/examine sender.Name, instead of just sender.

     


  • MattH6

    Whenever I do so I always use Sender.Name. As long as it is a Windows Control Object it has a name value which you can then do a select case on.


  • Santiago Caceres

    Ok thank you cgraus. You posted when I was still typing. I was wondering if tag was going to ever be useful.


  • 2dipikke

    My favorite trick in life is to store very complex datastructures in the tag.

    I guess my own feeling is that an eventhandler that had multiple controls going through it is not well designed.

    Every instinct reels against an event handler with a poutpouri of control types.



  • Greg Shelton

    You misunderstood. The sender will be an object. Like:

    if sender.Equals(tbOne) then status.Text = "Nothing too interesting."

    But the sender is not always going to be a textbox. It can also be a TrackBar, a Checkbox, or a ComboBox.

    The sub gets called on mousemove. If that helps at all.


  • JDHill

     

    Dim s as string = directcast(sender,textbox).text

    select case s

    case allthecases

     end select

     



  • GrahamH

    I think he wants this:

    Dim tb as textbox = directcast(sender,textbox)

    select case tb

    case allthecases

    end select

    Or, I would do this:

    Dim s as string = directcast(sender,textbox).tag

    select case s

    case allthecases

    end select

    and set the tags on the text boxes to something meaningful.



  • Medo7061

    What I said will work - set the tag properties on all of the controls in question, and cast to Control instead of Textbox.



  • Manglesh

    Yeah, I have to admit, some of my early C# code suffered from my not having worked out that 'tag' was there, and what it was good for :-)



  • Kismet123

    That is exactly what I am doing. I am setting the status to display a certain string depending on which control currently has mouse focus.


  • Gerscht

    Yes, I admit that bit caught me by surprise as I expected it to be all the same control type at least. However, I can see it. He's setting the status text based on what control is being selected or used, from what I can tell.



  • Using sender as a case in Select Case Statement