C# Controls Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
You can use the Controls.Find
method to find a control by its name from a Windows Forms control collection. Here is an example:
Control[] foundControls = this.Controls.Find("textBox1", true); if (foundControls.Length > 0) { TextBox textBox1 = foundControls[0] as TextBox; // do something with textBox1 }
In the above example, we use the Controls.Find
method to find a control with the name "textBox1"
. The second parameter of the method specifies whether to search the child controls of the control recursively. We then check if any controls were found, and cast the first control to a TextBox
object, to use it in our code.
You can use the Controls.OfType
method to get all child controls of a certain type from a Windows Forms control collection. Here is an example:
IEnumerable<TextBox> textBoxes = this.Controls.OfType<TextBox>(); foreach (TextBox textBox in textBoxes) { // do something with textBox }
In the above example, we use the Controls.OfType
method to get all child controls of type TextBox
. We then iterate over the resulting collection using a foreach
loop, and perform some action with each TextBox
control.
Find control by name in WinForms:
Controls.Find
method.Control[] foundControls = this.Controls.Find("textBox1", true); if (foundControls.Length > 0 && foundControls[0] is TextBox) { TextBox textBox = (TextBox)foundControls[0]; // Do something with textBox }
Search for controls in Windows Forms application:
foreach (Control control in GetAllControls(this)) { // Do something with control } private IEnumerable<Control> GetAllControls(Control container) { foreach (Control control in container.Controls) { yield return control; foreach (Control child in GetAllControls(control)) { yield return child; } } }
WinForms get all controls of a form C#:
foreach (Control control in GetAllControls(this)) { // Do something with control }
Find controls by type in Windows Forms C#:
var buttons = GetAllControls(this).OfType<Button>(); foreach (Button button in buttons) { // Do something with button }
Recursive find controls in C# WinForms:
private IEnumerable<Control> GetAllControls(Control container) { foreach (Control control in container.Controls) { yield return control; foreach (Control child in GetAllControls(control)) { yield return child; } } }
Iterate through all controls in Windows Forms C#:
foreach (Control control in GetAllControls(this)) { // Do something with control }
Get specific control by type in C#:
TextBox textBox = GetAllControls(this).OfType<TextBox>().FirstOrDefault(); if (textBox != null) { // Do something with textBox }
Find controls by property value in WinForms C#:
var matchingControls = GetAllControls(this).Where(c => c.Text == "Hello"); foreach (Control control in matchingControls) { // Do something with control }
Searching controls by tag property in WinForms C#:
Tag
property of controls.var taggedControls = GetAllControls(this).Where(c => c.Tag != null && c.Tag.ToString() == "TagValue"); foreach (Control control in taggedControls) { // Do something with control }
Retrieve controls inside a specific container in C#:
foreach (Control control in GetAllControls(panel1)) { // Do something with control inside panel1 }
Enumerate child controls in Windows Forms C#:
Controls
property of the container.foreach (Control control in panel1.Controls) { // Do something with control }
Find controls with a specific attribute in C# WinForms:
var attributeControls = GetAllControls(this).Where(c => c is SomeCustomControl && ((SomeCustomControl)c).CustomAttribute == "AttributeValue"); foreach (Control control in attributeControls) { // Do something with control }
Get control by index in WinForms C#:
Controls
collection.if (this.Controls.Count > 0) { Control control = this.Controls[0]; // Do something with control }
Search for controls in a UserControl C#:
foreach (Control control in GetAllControls(userControl1)) { // Do something with control inside userControl1 }
Filter controls based on certain criteria in C#:
Predicate<Control> criteria = c => c is TextBox && c.Enabled; var filteredControls = GetAllControls(this).Where(criteria); foreach (Control control in filteredControls) { // Do something with filtered control }