Find Control by Name from Windows Forms Controls

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!";
}

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.

Get a Windows Forms control by name in C#

Use the Control.ControlCollection.Find method.

Try this:

this.Controls.Find()

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";
}

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.
}
}

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

Screenshot of ComboBox of properties window, closed and opened

C# control.find explanation

ControlCollection.Find returns a Control[], so possibly multiple controls. The reason is that you could have multiple controls on the form with the same name.

Your second parameter is a bool which you have set to true. It indicates whether this control should be searched recursively or only in the parent control.

So your code is safe if you never have two controls with the same name.

Otherwise you should

  1. avoid duplicate names
  2. or, if it is desired, use a loop to iterate these controls instead of accesssing only the first

Note that you can't add controls with the same name via designer which detects duplicate names. But you are free to add controls with the same name programmatically.



Related Topics



Leave a reply



Submit