Help with changing the color of a label

UberDan

Newcomer
Joined
Nov 27, 2005
Messages
3
Ok, so I have a string variable (strSeatName) that holds the name of a label (lblSeat1) and I'm trying to change the color of that label without hard-coding the label name in the command (since it's inside a For loop that changes multiple labels named lblSeat1, lblSeat2, lblSeat3, etc...).

Here is the command I need to use:

lblSeat1.BackColor = System.Drawing.Color.LawnGreen

Except that I need to get the part where it says "lblSeat1" out of the string variable.

I'm sure this is easy. I'm just a n00b.

Suggestions?
 
Actually it's not as easy as you think. But everyone always thinks it will be, I know I did when I first started programming. As far as I know there is no way to call a control usins a string without looping. Something like the following should work.
C#:
string myString = "lblOne";
foreach(Control ctrl in this.Controls)
{
	if(ctrl.Name == myString)
	{
		// do something
	}
}
 
Last edited:
Cags said:
Actually it's not as easy as you think. But everyone always thinks it will be, I know I did when I first started programming. As far as I know there is no way to call a control usins a string without looping. Something like the following should work.
C#:
string myString = "lblOne";
foreach(Control ctrl in this.Controls)
{
	if(ctrl.Name == myString)
	{
		// do something
	}
}
Yeah, I talked to my professor and he's not looking for anything pretty, which is good because I now have 18 If / ElseIf checks controlling it instead of a simple For loop. lol

Curse you, VB!
 
The above code can be written in VB as the following...
Visual Basic:
        Dim myString As String = "lblOne"
        For Each ctrl As Control In Me.Controls
            If ctrl.Name = myString Then
                ' something here
            End If
        Next
As a side note, if you post the language you are using, or alternatively add it to your signature, then you may get a response you understand faster.
 
That's the poopy thing about compiled programming languages. The variable names you used when writing the program disappear at runtime so you can't access variables with their programatic name on the fly.

You could skip the looping and cheat a little if you felt like learning about reflection, but that is hardly worth the trouble just to change the color of a label.

In the mean time, if you want something quick and easy, you could always make a function that will do the looping for you...
Visual Basic:
'Overload to save us some typing
Public Function GetControl(Name As String) As Control
    Return GetControl(Me, Name)
End Function
 
'Mmmmmmmmm... recursive
Public Function GetControl(Container As Control, Name As String) As Control
    For Each C As Control In Container.Controls
        ' If this is the control, return it
        If C.Name = Name Then Return C
 
        ' If not, search child controls and return the control if we find it
        Dim Result As Control = GetControl(C, Name)
        If Not Result Is Nothing Then Return Result 
        'We could also try out the IsNot operator, but that isn't kosher with VB7
    Next
    
    Return Nothing 'if we find nothing
End Function
 
'Use it like this:
GetControl("lblSeat42").BackColor = Colors.SmurfBlue

Another nifty thing new to .Net 2.0 is the fact that alot of things are organized by indecies and keys. You can write code like this without writing any of your own helper functions:
Visual Basic:
Me.Controls("lblSeat22").BackColor = Colors.HotPink
The only problem is that this won't work for nested controls (such as a control within a group box), and it is only available on VB 8/C# 2.
 
Cags said:
The above code can be written in VB as the following...
Visual Basic:
        Dim myString As String = "lblOne"
        For Each ctrl As Control In Me.Controls
            If ctrl.Name = myString Then
                ' something here
            End If
        Next
As a side note, if you post the language you are using, or alternatively add it to your signature, then you may get a response you understand faster.
Oops, my mistake.... VB .NET

As for that bit of code, I'm not sure I understand it.

This is what it needs it to do:

Code:
Dim intCounter As Int
Dim strSeatName As String

For intCounter = 1 To 18 Step 1

strSeatName = "lblSeat" & intCounter

DsSeating1.Clear()
OleDbDataAdapter1.SelectCommand.CommandText = "select * from seating where seat_no = " & intCounter & " and perf_date = '" & lstDate.Text & "'"
OleDbDataAdapter1.Fill(DsSeating1)

If DsSeating1.Tables(0).Rows.Count = 0 Then

(strSeatName data should be here).BackColor = System.Drawing.Color.LawnGreen
(strSeatName data should be here).ForeColor = System.Drawing.Color.Black

End If
Next
 
Using Marbles methods (as they were more complete than my example), you would use it as follows.
Visual Basic:
        Dim intCounter As Int
        Dim strSeatName As String

        For intCounter = 1 To 18 Step 1

            strSeatName = "lblSeat" & intCounter

            DsSeating1.Clear()
            OleDbDataAdapter1.SelectCommand.CommandText = "select * from seating where seat_no = " & intCounter & " and perf_date = '" & lstDate.Text & "'"
            OleDbDataAdapter1.Fill(DsSeating1)

            If DsSeating1.Tables(0).Rows.Count = 0 Then
                Dim ctrl As Control = GetControl(strSeatName)
                ctrl.BackColor = System.Drawing.Color.LawnGreen
                ctrl.ForeColor = System.Drawing.Color.Black
            End If
        Next
 
Back
Top