Answered by:
Using sender as a case in Select Case Statement

Question
-
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?Tuesday, March 21, 2006 11:06 PM
Answers
-
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.
Tuesday, March 21, 2006 11:35 PM
All replies
-
Dim s as string = directcast(sender,textbox).text
select case s
case allthecases
end select
Tuesday, March 21, 2006 11:24 PM -
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.
Tuesday, March 21, 2006 11:35 PM -
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.Tuesday, March 21, 2006 11:39 PM -
What I said will work - set the tag properties on all of the controls in question, and cast to Control instead of Textbox.
Tuesday, March 21, 2006 11:43 PM -
Ok thank you cgraus. You posted when I was still typing. I was wondering if tag was going to ever be useful.Wednesday, March 22, 2006 12:03 AM
-
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 :-)
Wednesday, March 22, 2006 12:09 AM -
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.
Wednesday, March 22, 2006 2:31 AM -
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.
Wednesday, March 22, 2006 2:33 AM -
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.Wednesday, March 22, 2006 6:04 AM
-
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 SelectHowever, 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 IfI don't know if this helps.
Wednesday, March 22, 2006 2:43 PM -
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.
Wednesday, March 29, 2006 10:58 PM -
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.
Friday, March 31, 2006 9:22 PM -
As an alternative to the answers above, this also works well
Select Case sender.ToString
Case ControlItem1.Tag
Case ControlItem2.Tag
End Select
Set the tags accordingly
Wednesday, January 26, 2011 12:49 PM