0

in my windows form application there are multiple textBoxes . How can I delete content of all of theme? I don't want to delete content of textbox one by one with

textBox1.text=string.emty.tostring();
user1837982
  • 93
  • 1
  • 1
  • 7
  • You can get all the [textboxes recursively](http://stackoverflow.com/questions/2525062/how-can-i-query-all-childcontrols-of-a-winform-recursively) and then iterate through them setting the Text property to `""` or `string.Empty` – Habib Apr 24 '13 at 10:03

2 Answers2

0
foreach (Textbox myTB in this.Controls)
{
   if (myTB != null)
       myTB.Text = String.Empty;
}

You don't have to toString() on Empty.

0

If all these TextBoxes belong to the same container and no other TextBoxes are placed in this container, then you can just enumerate container's children

foreach (var tb in container.Controls.OfType<TextBox>())
{
    tb.Text = string.Empty; // or tb.Text = null;
}

If you have multiple textboxes and for some reason you cannot group them in one container you shoud recursively search for all available textboxes and then filter the resulting collection.
You can use Tag roperty for this.

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137