Find control by name from Windows Forms controls
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!";
}
Get a Windows Forms control by name in C#
Use the Control.ControlCollection.Find method.
Try this:
this.Controls.Find()
Find a control in Windows Forms by name
You can use the form's Controls.Find() method to retrieve a reference back:
var matches = this.Controls.Find("button2", true);
Beware that this returns an array, the Name property of a control can be ambiguous, there is no mechanism that ensures that a control has a unique name. You'll have to enforce that yourself.
Find Windows Forms control by name using a pattern
You could search for them with LINQ via:
var matches = control.Controls.Cast<Control>()
.Where(c => c.Name.StartsWith("btnOverlay"));
The Cast<T>
call is required, as ControlCollection
does not implement IEnumerable<T>
, only IEnumerable
. Also, this doesn't do a recursive search, but only searches the contained controls directly. If recursion is required, you'll likely need to refactor this into a method similar to this answer.
Get all controls with names that start with specific string
You can use Linq's where method:
foreach (Label l in this.Controls.OfType<Label>().Where(l => l.Name.StartsWith("tafel_noemer_")))
{
l.Text = "bla bla";
}
How can I find a control looking at windows designer in visual studio 2012?
Use the document outline pane (Ctrl + Alt + T) which will include searching a little in the document tree, or the ComboBox in the top of properties pane, which is sorted alphabetically.
Screenshot of ComboBox of properties window, closed and opened
Find a specific control by name and modify its .Value property
Assuming that your progressbar control is named "test" (all lowercase letters) and is placed directly on the surface of your form (not inside a groupbox,panel or other control container) then this code should work and simplify your work
foreach (var c in this.Controls.OfType<ProgressBar>().Where(x => x.Name == "test")
{
c.Value = 23;
}
instead if the ProgressBar is placed inside a control container (like a panel) the above code should be changed to loop over the controls collection of the container
foreach (var c in this.panel1.Controls.OfType<ProgressBar>().Where(x => x.Name == "test")
{
c.Value = 23;
}
As pointed out in the comment by KingKing, if you are absolutely sure that a control named "test" exists in your groupbox then a simple lookup in the controls collection should result in your progressbar. Looping is not necessary in this case
ProgressBar pb = this.groupBox1.Controls["test"] as ProgressBar;
if(pb != null) pb.Value = 23;
Find multiple controls with by partially matching their name
You can loop through the Controls collection of the form and just check the name of each control that it contains something like 'Label'. or you could check that the control is a typeof TextBox, Label, etc.
E.g.
foreach (Control control in form.Controls)
{
if (control.Name.ToUpper().Contains("[Your control search string here]"))
{
// Do something here.
}
if (control is TextBox) {
// Do something here.
}
}
Related Topics
Simple State MAChine Example in C#
Redirect from Action Filter Attribute
Does Parallel.Foreach Limit the Number of Active Threads
Access to the Path Is Denied When Using Directory.Getfiles(...)
The Difference Between Try/Catch/Throw and Try/Catch(E)/Throw E
Deserializing JSON to Abstract Class
Trying to Use the C# Spellcheck Class
C# How to Translate Virtual Keycode to Char
How to Find and Replace Text in a File
Error - Unable to Access the Iis Metabase
How to Wait for a Backgroundworker to Cancel
How to Implement Icomparable Interface
Generate a PDF That Automatically Prints
How to Create Custom Config Section in App.Config
Correct, Idiomatic Way to Use Custom Editor Templates with Ienumerable Models in ASP.NET MVC