Control Reference from String

  • Thread starter Thread starter Barry
  • Start date Start date
B

Barry

Hi

I have programmatically create about 5 TextBox controls on a Windows Form ,
their name are "txt1", "txt2", "txt3" etc

which method/function return a reference to the TextBox control given the
name "txt1" etc;

something like
TextBox txtBox1 = (TextBox)Control.FromName("txt1");

TIA
Barry
 
Hello,

try this:

Control result;
foreach (Control ctrl in control.Controls) {
if (ctrl.Name != "txt1") continue;

result = ctrl;
break;
}

Of course, this will only return objects directly below the control. You
must recusively traverse the control hierachy.

Best regards,
Henning Krause
 
Back
Top