0

Instead of changing my 15 textboxes manually, I would like to do it automatically, maybe doing a foreach loop? I don't know what do you call it but this kind of method is usually done when clearing textboxes and controls that is instead of putting a .clear() to all textbox, a method or a loop will be made and all of them would be cleared. How to do this? Sorry if this is somewhat confusing, for I am still a newbie.

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by AngelicOne
Featured Replies
  • 1. You can create an array of all wanted textBoxes: [CODE] private void button1_Click(object sender, EventArgs e) { TextBox[] tb = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5 }; for (int i = 0; i < tb.Length; i++) tb[i].Text = String.Empty; } [/CODE] Read More

  • 2. To loop through all controls, and if textBox is found, do something: [CODE] private void button2_Click(object sender, EventArgs e) { foreach (Control c in Controls) { if (c is TextBox) c.Text = String.Empty; } } [/CODE] Read More

  • Use: c.Enable = false; if you want to set the control (textBox) not to be editable. Read More

1

1. You can create an array of all wanted textBoxes:

private void button1_Click(object sender, EventArgs e)
        {
            TextBox[] tb = new TextBox[] { textBox1, textBox2, textBox3, textBox4, textBox5 };
            for (int i = 0; i < tb.Length; i++)
                tb[i].Text = String.Empty;
        }
1

2. To loop through all controls, and if textBox is found, do something:

private void button2_Click(object sender, EventArgs e)
        {
            foreach (Control c in Controls)
            {
                if (c is TextBox)
                    c.Text = String.Empty;
            }
        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.