c# - Foreach Control in form, how to do something to all the TextBoxes in Form?

C# - Foreach Control in form, how to do something to all the TextBoxes in Form?

In C#, to perform operations on all TextBox controls in a Form, you can iterate through the controls collection of the Form and specifically target TextBox controls. This can be achieved using a foreach loop to examine each control and apply operations to those that are TextBox instances.

Example

Here's a step-by-step example of how to iterate through all TextBox controls on a Form and perform an operation on each of them:

using System; using System.Windows.Forms; namespace WindowsFormsApp { public class MainForm : Form { public MainForm() { // Add some TextBox controls to the form this.Controls.Add(new TextBox { Name = "textBox1", Text = "TextBox 1" }); this.Controls.Add(new TextBox { Name = "textBox2", Text = "TextBox 2" }); this.Controls.Add(new TextBox { Name = "textBox3", Text = "TextBox 3" }); // Call the method to perform an action on all TextBox controls PerformActionOnTextBoxes(); } private void PerformActionOnTextBoxes() { // Iterate through all controls on the form foreach (Control control in this.Controls) { // Check if the control is a TextBox if (control is TextBox textBox) { // Perform your action on the TextBox control textBox.Text = "Modified Text"; // Example action } } } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } } 

Explanation

  1. Add Controls to Form:

    • The MainForm constructor adds several TextBox controls to the form.
  2. Iterate and Perform Action:

    • The PerformActionOnTextBoxes method iterates through the Controls collection of the Form.
    • The is keyword is used to check if the control is a TextBox.
    • If it is a TextBox, you can perform the desired operation (in this case, modifying the text).
  3. Run the Application:

    • The Main method sets up and runs the Form.

Additional Notes

  • Nested Controls:

    • If your TextBox controls are nested within other container controls (e.g., GroupBox, Panel), you need to recursively iterate through the controls of those containers as well. This can be done using a recursive function.
    private void PerformActionOnTextBoxes(Control parentControl) { foreach (Control control in parentControl.Controls) { if (control is TextBox textBox) { textBox.Text = "Modified Text"; // Example action } else if (control.HasChildren) { PerformActionOnTextBoxes(control); // Recursively process child controls } } } 
  • Form Controls Collection:

    • The Controls property of a Form (or any other container control) returns a collection of all child controls.

This approach ensures that you can efficiently handle multiple TextBox controls and apply consistent changes or actions across them in a Windows Forms application.

Examples

  1. "How to set the same text for all TextBoxes in a Windows Form using C#?"

    Description: This query focuses on setting the same text for all TextBoxes in a form.

    Code:

    using System; using System.Windows.Forms; public partial class MainForm : Form { public MainForm() { InitializeComponent(); SetTextForAllTextBoxes("Default Text"); } private void SetTextForAllTextBoxes(string text) { foreach (Control control in this.Controls) { if (control is TextBox textBox) { textBox.Text = text; } } } } 
  2. "How to clear all TextBoxes in a form when a button is clicked in C#?"

    Description: This query is about clearing the text of all TextBoxes when a button is clicked.

    Code:

    using System; using System.Windows.Forms; public partial class MainForm : Form { public MainForm() { InitializeComponent(); Button clearButton = new Button { Text = "Clear" }; clearButton.Click += ClearButton_Click; this.Controls.Add(clearButton); } private void ClearButton_Click(object sender, EventArgs e) { foreach (Control control in this.Controls) { if (control is TextBox textBox) { textBox.Clear(); } } } } 
  3. "How to enable or disable all TextBoxes on a form in C#?"

    Description: This query involves enabling or disabling all TextBoxes in a form.

    Code:

    using System; using System.Windows.Forms; public partial class MainForm : Form { public MainForm() { InitializeComponent(); Button disableButton = new Button { Text = "Disable TextBoxes" }; disableButton.Click += DisableButton_Click; this.Controls.Add(disableButton); } private void DisableButton_Click(object sender, EventArgs e) { foreach (Control control in this.Controls) { if (control is TextBox textBox) { textBox.Enabled = false; } } } } 
  4. "How to change the background color of all TextBoxes in a form using C#?"

    Description: This query deals with changing the background color of all TextBoxes.

    Code:

    using System; using System.Drawing; using System.Windows.Forms; public partial class MainForm : Form { public MainForm() { InitializeComponent(); Button colorButton = new Button { Text = "Change Color" }; colorButton.Click += ColorButton_Click; this.Controls.Add(colorButton); } private void ColorButton_Click(object sender, EventArgs e) { foreach (Control control in this.Controls) { if (control is TextBox textBox) { textBox.BackColor = Color.LightYellow; } } } } 
  5. "How to validate all TextBoxes in a form to ensure they are not empty in C#?"

    Description: This query focuses on validating that none of the TextBoxes are empty.

    Code:

    using System; using System.Windows.Forms; public partial class MainForm : Form { public MainForm() { InitializeComponent(); Button validateButton = new Button { Text = "Validate" }; validateButton.Click += ValidateButton_Click; this.Controls.Add(validateButton); } private void ValidateButton_Click(object sender, EventArgs e) { foreach (Control control in this.Controls) { if (control is TextBox textBox && string.IsNullOrWhiteSpace(textBox.Text)) { MessageBox.Show("Please fill all text boxes.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } MessageBox.Show("All text boxes are filled.", "Validation Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } } 
  6. "How to iterate through all child TextBoxes within a specific container (e.g., GroupBox) in C#?"

    Description: This query involves iterating through all TextBoxes within a specific container control like a GroupBox.

    Code:

    using System; using System.Windows.Forms; public partial class MainForm : Form { private GroupBox groupBox1; public MainForm() { InitializeComponent(); groupBox1 = new GroupBox { Text = "Group 1" }; this.Controls.Add(groupBox1); Button processButton = new Button { Text = "Process GroupBox TextBoxes" }; processButton.Click += ProcessButton_Click; this.Controls.Add(processButton); } private void ProcessButton_Click(object sender, EventArgs e) { foreach (Control control in groupBox1.Controls) { if (control is TextBox textBox) { textBox.BackColor = Color.LightGreen; } } } } 
  7. "How to find and update the text of a specific TextBox by name in a Windows Form?"

    Description: This query is about finding a specific TextBox by its name and updating its text.

    Code:

    using System; using System.Windows.Forms; public partial class MainForm : Form { public MainForm() { InitializeComponent(); TextBox myTextBox = new TextBox { Name = "myTextBox", Text = "Initial" }; this.Controls.Add(myTextBox); Button updateButton = new Button { Text = "Update TextBox" }; updateButton.Click += UpdateButton_Click; this.Controls.Add(updateButton); } private void UpdateButton_Click(object sender, EventArgs e) { TextBox myTextBox = this.Controls["myTextBox"] as TextBox; if (myTextBox != null) { myTextBox.Text = "Updated Text"; } } } 
  8. "How to enable or disable all TextBoxes based on a checkbox status in C#?"

    Description: This query involves enabling or disabling all TextBoxes based on the state of a checkbox.

    Code:

    using System; using System.Windows.Forms; public partial class MainForm : Form { private CheckBox enableCheckBox; public MainForm() { InitializeComponent(); enableCheckBox = new CheckBox { Text = "Enable TextBoxes", Checked = true }; enableCheckBox.CheckedChanged += EnableCheckBox_CheckedChanged; this.Controls.Add(enableCheckBox); } private void EnableCheckBox_CheckedChanged(object sender, EventArgs e) { bool isEnabled = enableCheckBox.Checked; foreach (Control control in this.Controls) { if (control is TextBox textBox) { textBox.Enabled = isEnabled; } } } } 
  9. "How to apply a font size change to all TextBoxes in a form in C#?"

    Description: This query focuses on changing the font size of all TextBoxes in a form.

    Code:

    using System; using System.Drawing; using System.Windows.Forms; public partial class MainForm : Form { public MainForm() { InitializeComponent(); Button fontButton = new Button { Text = "Change Font Size" }; fontButton.Click += FontButton_Click; this.Controls.Add(fontButton); } private void FontButton_Click(object sender, EventArgs e) { foreach (Control control in this.Controls) { if (control is TextBox textBox) { textBox.Font = new Font(textBox.Font.FontFamily, 14); } } } } 
  10. "How to handle all TextBoxes within nested containers (e.g., Panels) in a Windows Form using C#?"

    Description: This query involves handling all TextBoxes within nested containers, such as Panels.

    Code:

    using System; using System.Windows.Forms; public partial class MainForm : Form { private Panel panel1; public MainForm() { InitializeComponent(); panel1 = new Panel(); this.Controls.Add(panel1); Button processButton = new Button { Text = "Process Nested TextBoxes" }; processButton.Click += ProcessButton_Click; this.Controls.Add(processButton); } private void ProcessButton_Click(object sender, EventArgs e) { ProcessTextBoxes(this); } private void ProcessTextBoxes(Control parent) { foreach (Control control in parent.Controls) { if (control is TextBox textBox) { textBox.BackColor = Color.LightBlue; } else if (control.HasChildren) { ProcessTextBoxes(control); } } } } 

More Tags

master-slave amazon-s3 xmllint chromium angular-cli-v6 m3u8 scapy swiperefreshlayout flutter-streambuilder assets

More Programming Questions

More Chemical reactions Calculators

More Cat Calculators

More Stoichiometry Calculators

More General chemistry Calculators