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.

Recommended Answers

All 5 Replies

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

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

I can't put the readonly extension on c. Am I missing something?

Use:
c.Enable = false;

if you want to set the control (textBox) not to be editable.

commented: tnx 4 help +1

I added TextBox tb = (TextBox)c and it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.