views:

80

answers:

3

Hi to all,

I have a panel, where my program is adding dynamic controls. Either I want to access these controls at runtime for changing their colors or texts.

the only way I know is:

Control [] myControls = myPanel.Controls.Find( name , true );

Here the problem is, my dynamic Controls have not any name ! Their name are " null ". If I'm trying name as a null value, it gives error. How can I achieve it ? Must I give every added control a name ?

Thanks.

A: 

Maybe you can just keep a reference (e.g. instance field, within a dictionary, etc.) to each dynamically added control, and use this when you'll need to access the controls later.

Romain Verdier
+1  A: 

You could loop through the Controls collection:

 foreach(var control in myPanel.Controls) {

    //Here you do something with the appropriate control.
 }
Robban
+1  A: 

Could just do something like this:

foreach(Control control in myPanel.Controls)
  control.Backcolor=Color.Black;
Blindy
thanks it's working. Should I use .GetType to learn the kind of my Control if it is a button or a label or something else ?
Cmptrb
You can du a "if (control is Button) { ((Button)control).blahblahblah; } else if (control is label) { (((Label)control).blah_blah_blah"; }etc....
TcKs
Yes, the `is` operator tests if a class is a subclass of another class, or in this case if your actual instance is of a more specific class than just a generic `Control`.
Blindy