how to a refernce to a control dynamically

  • Thread starter Thread starter Bronco
  • Start date Start date
B

Bronco

Hi all,

I'm trying to achieve something like this:

<codesnipet>
Label label;
string labelref = "labelName";

label = form.Controls[labelref];
// etc...
</codesnipet>

Obviously this won't work because Controls only takes an integer. How do can
do this?

TIA,

Bronco
 
Bronco said:
Obviously this won't work because Controls only takes an integer.
How do can do this?

Use For Each to iterate through all controls. Either check the
Control.Name or, check if the type is a Label and cast to Label
if you wish to find it by Label.Text
 
Thanks.

I have to to mention that this was the solution that I was trying to avoid
because it seems rather costly from a performance stand of view...
I will elaborate some more. What I'm trying to achieve is some sort of UI
enhancer which repositions all label next to thier respective controls
(according to the rules I set, I'm trying to do this by name, at least now).

TIA,

Bronco
 
Bronco said:
Thanks.

I have to to mention that this was the solution that I was trying to avoid
because it seems rather costly from a performance stand of view...
I will elaborate some more. What I'm trying to achieve is some sort of UI
enhancer which repositions all label next to thier respective controls
(according to the rules I set, I'm trying to do this by name, at least now).

For a special tool like this, you might increase performance by
creating your own data structure for managing controls and initializing
this when the Form is created (e.g. add all labels to a Hashtable,
using the name as key).
 
Back
Top