How can I find and modify the correct control on my form?

Hello,

say, if you have several of labels (Label1, Label2, ..., Labeln) on your form, and also a textbox and a button. If you put, for instance, "label5" in the textbox and click the button, how is it then possible to find Label5 and, say, change its back color?

Hope it does not sound too cryptic...

Any suggestions? :-)

[356 byte] By [RookieBoy] at [2007-12-22]
# 1
You can use the Form.Controls.ContainsKey("label5") to find the control on the form based on the name "label5".
TimothyNgMSFT at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
Hi,
Try the following method :
Me.Controls(TextBox1.Text).BackColor = Color.Red
And if you want to access a special property for Label control :
CType(Me.Controls(TextBox1.Text), Label).BackColor = Color.Red
HTH
MoayadMardini at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

I tried "Me.Controls(TextBox1.Text).BackColor = Color.Red", but it gave me the error "Cast from string "Label5" to type 'Integer' is not valid.". The Me.Controls(...)-statement requires an index (integer).

I also tried the following: Me.Controls(Me.Controls.IndexOf(Textbox1.Text)), but that also gave me an error: "Value of type 'String' cannot be converted to 'System.Windows.Forms.Control".

Any suggestions...?

My .NET version is 1.1.

RookieBoy at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

I tried "Me.Controls(Text1.Text).BackColor = RGB(255, 0, 0)", it changed the color of control sucessfully. .

Thinkerwu at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

The overload of the the ControlCollection.Item property that takes a string is new to .NET 2.0 so you won't be able to find it that way. I looked for alternative members on the ControlCollection that would relate the controls to a key but it seems all of the key related members are v2.0 specific. you'll have to manually seach the controls collection comparing each item by it's name property.

This method should do the trick:

Function TryGetControlByName(Of T As Control) _
(container As Control, name As String, _
ByRef controlT As T, recurse As Boolean) As Boolean

For Each child As Control In container.Controls
controlT = TryCast(child, T)
If (controlT IsNot Nothing AndAlso
controlT.Name = name) OrElse _
(recurse AndAlso
TryGetControlByName(child, name, controlT, True)) _
Then Return True
Next child

Return False
End Function

You can call it thusly:

Dim lblTarget As Label
If TryGetControlByName(Me, "name", lblTarget, True) Then lblTarget.BackColor = color

You could of course wrap this in some sort of Function TrySetControlBackColorByName(name As String, color As Color) As Boolean but that might just be overkill. The above method is quite flexible though. Hope this helps :)

AnthonyD.Green at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

As an aside it's fairly unlikely that the user will know, understand, or give a hoot about the names of your controls but I imagine you have a strategy for this. You just have to be careful that your controls are ordered on the form that way the user expects. The WinForms has a mode to alter z-order if you have to. You might be safer making some sort of control array wrapper around a dynamically created list of labels. More work for you upfront but much more flexible in the long run.

AnthonyD.Green at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7

Iterate through the controls collection until you find the control you are looking for....

For Each ctrl As Control In Me.Controls

If ctrl.Name = "Label5" Then

ctrl.BackColor = Color.Blue

End If

Next

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8

I am such a spoiled and flaming idiot. Leave it to me to give a solution to a v1.1 problem that depends heavily on 2.0 langauge features like Generics and TryCast. Just goes to show the dangers of overdesign (and really cool language features). Oh well. Anyone using .NET 2.0 need a recursive generic control searcher that searches by name and type? :(

AnthonyD.Green at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9

Goody-goody, problem solved :-) Thanks DMan1!!

Anthony D. Green,

I tried your solution, but I couldn't make it work - VB gave me error on '(Of T as Control)' and also on 'controlT = TryCast(child, T)', i.e. the TryCast-statement. I tried to sort it out by modifying and searhing help, but with no luck...

Anyhow, thanks to you both :-)

RookieBoy at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10

Look up two post, it's explains why my solution couldn't work for you and how funny - in a stupid way - it was that I put so much thought into it. In summary my solution was full of VB8 ONLY language features and you're using VB1.1. Stupid me.

In .NET 2.0 it's a beautiful solution, haha. I could have given you some 1.1 compliant code but you have a work solution already and it would have lacked the elegance to really be worth it. At least you got help though.

AnthonyD.Green at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic General...