2

I have set all my TextBoxs.readOnly = true; in the form by default. But when an Edit Button is clicked. I want it to make all the TextBoxs.readOnly = false;

I have tried;

private void btnEdit_Click(object sender, EventArgs e)
{
    foreach (var textBox in this.Controls.OfType<TextBox>())
        textBox.ReadOnly = false;
}

Each TextBox has a unique name. The easiest way to do it would be this, below. But I don't want to do it that way.

txtName.ReadOnly = false;
txtAddress.ReadOnly = false;
...
Dmitry
  • 13,797
  • 6
  • 32
  • 48
AndroidAL
  • 1,111
  • 4
  • 15
  • 35
  • With this approach you can set just `TextBoxes` that are placed directly in your form not those that are inside another container like `GroupBox` or `Panel`. You need a recursive method for this purpose see here:http://stackoverflow.com/a/34676012/2946329 – Salah Akbari Jan 12 '16 at 12:35
  • all the `TextBoxes` are inside a `GroupBox` – AndroidAL Jan 12 '16 at 12:35
  • @user2946329 is right, but I'd prefer either [this](http://stackoverflow.com/a/3426721/1997232) recursive method or simply hold controls references in dedicated array. If controls are inside same container, then replace `this` with that container instance (in your case that `GroupBox`). – Sinatr Jan 12 '16 at 12:37

1 Answers1

3

With this approach you can set just TextBoxes's ReadOnly that are placed directly in your form not those that are inside another container like GroupBox or Panel. You need a recursive method for this purpose if you have more than one container (example1, example2). But Since all your TextBoxes are inside one GroupBox then simply replace this with your GroupBox's Name like this:

foreach (var textBox in groupBox1.Controls.OfType<TextBox>())
    textBox.ReadOnly = false;
Community
  • 1
  • 1
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109