Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find control by name from Windows Forms controls

Tags:

c#

.net

winforms

I have a list of my textbox names, and I want to find a control by name. How is it possible?

like image 497
krunal shah Avatar asked Oct 10 '10 01:10

krunal shah


People also ask

How do you access a form control from another form?

Control c = TheForm("Form1"); Once we have this, we can gain access to ALL the child controls including the children in other container controls on the form. The next method to declare is the one that locates the control to access on the form returned by the TheForm method.

What are the controls of Windows form?

Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side, Windows-based applications. Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls.

What is control name?

A control's "control name" is given by its name attribute. The scope of the name attribute for a control within a FORM element is the FORM element. Each control has both an initial value and a current value, both of which are character strings.

Which property is useful to provide name for a control?

The Name property can be used at run time to evaluate the object by name rather than type and programmatic name. Because the Name property returns a String type, it can be evaluated in case-style logic statements ( Select statement in Visual Basic, switch statement in Visual C# and Visual C++).


1 Answers

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!"; } 
like image 57
bla Avatar answered Oct 01 '22 09:10

bla