CEMc

I am creating multiple labels during runtime and giving them specific names. How do I use these names to access the values related to each label.

For instance, I create 20 labels entitled Player##, where ## is a number from 00 through 99. I then want to change the Visible or Enabled properties of these labels that have names, preferably by doing something along the lines of ("Player"+playernum).Visible=true. Is there anyway to do this simply I can use arrays and the like, but would rather not have to search through an array every time I want to access this component since it has a name assigned to it.



Re: Visual C# General Accessing components created during runtime

Brendan Grant

Because all of your controls would be contained within some kind of container (Form, Panel, GroupBox, etc)... you can use the containers Controls property and look it up based on name like so:

Label l = new Label();
l.Text =
"Hello World";
l.Name =
"MyLabel";

this.Controls.Add(l);

l = null;

l = (Label)this.Controls["MyLabel"];
l.Text =
"Foo";

Remember that when we look it up by name, we are looking based on the value of the controls Name property, not the variable name in code.

Is this what you are looking for






Re: Visual C# General Accessing components created during runtime

Vivek Ragunathan

Hi

You can maintain a collection of the Labels that you create and add, say List<Label>. And then use that to perform a lookup. Or use the Form.Controls property.

Regards





Re: Visual C# General Accessing components created during runtime

Sanophy

Honestly, after trying to do this myself, the best option is to make an array of labels...
Name the array Player, and the index can be your number...