Control Reference from String

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
 
H

Henning Krause [MVP - Exchange]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top