tags:

views:

48

answers:

2

I have list of my textbox names.

I want to find control by name.

How it possible ?

A: 

You can use:

f.Controls[name];

Where f is your form variable. That gives you the control with name name.

CesarGon
Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
Michael Petrotta
@Michael: That is correct.
CesarGon
+3  A: 

Use Control.ControlCollection.Find.

TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox; tbx.Text = "found!";

EDIT for asker:

Control[] tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
if (tbxs != null && tbxs.Length > 0)
{
    tbxs[0].Text = "Found!";
}
Lee Sy En
TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
krunal shah
Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
krunal shah
Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
Lee Sy En
No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
krunal shah
The extension method 'FirstOrDefault' does not exist in .net 2.0.You need to be using .net 3.5 or later for that to work. Just remove the .FirstOrDefault() and the provided code should work.
Tony Abrams
TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
Lee Sy En
Change .FirstOrDefault() to [0]
Lee Sy En