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? :-)
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
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.
I tried "Me.Controls(Text1.Text).BackColor = RGB(255, 0, 0)", it changed the color of control sucessfully. .
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 :)
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.
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 :-)
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.