3

i just started learning C# and i was wondering if it's possible to clear the content of all the textboxes for example in my form.

I know how to clear one by one but it's not practical for me.

thanks!

edit: I found my answer thank you all very much

Ahmed Aboumalek
  • 613
  • 4
  • 21
Àndres
  • 121
  • 1
  • 1
  • 9

7 Answers7

4

If you only want to clear TextBoxes you can do it easily like this:

foreach(TextBox tb in this.Controls.OfType<TextBox>())
    tb.Text = string.Empty;

But that will only clear all the TextBoxes that are directly on the Form and not inside a GroupBox or Panel or any other container.

If you have TextBoxes inside other containers you need to recurse:

private void ClearTextBoxes(ControlCollection controls)
{
    foreach(TextBox tb in controls.OfType<TextBox>())
        tb.Text = string.Empty;
    foreach(Control c in controls)
        ClearTextBoxes(c.Controls);
}

Call this like ClearTextBoxes(this.Controls);

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • I didn't know you can use OfType, this is certainly new for me, very interesting, thank you a lot sir ( i can't vote you up sadely ) – Àndres Apr 01 '16 at 16:35
4

You can use the following loop to clear all the textbox objects in your active form:

foreach (Control c in this.Controls) 
{
    if (c.GetType() == typeof(TextBox))
    {
        c.Clear();
    }
}

You can also use the loop inside a function and give it a Boolean return value to test if it was successfuly executed.

Orkun Bekar
  • 1,447
  • 1
  • 15
  • 36
Ahmed Aboumalek
  • 613
  • 4
  • 21
  • I read this somewhere I think but didn't remember it, this is exactly what I was looking for, can I use this to clear all the textboxes for example in all the forms? – Àndres Apr 01 '16 at 16:29
  • 1
    edit: not sure actually, try removing the keyword `this` in `this.Controls`, might do the trick. Maybe another person can help you with it – Ahmed Aboumalek Apr 01 '16 at 17:19
1

I bet you are looking for a solution similar to this one : Foreach Control in form, how can I do something to all the TextBoxes in my Form?

What you want to do is loop through all the controls in the Form, if the control is a TextBox you want to clear it

Community
  • 1
  • 1
MrSykkox
  • 528
  • 4
  • 14
1
private void ClearTextBoxes(ControlCollection controls)
{
    foreach(control c in Controls)
        if(c is TextBox)
            c.Clear();
}
Olemak
  • 2,005
  • 1
  • 17
  • 20
0

If you need to clear just what you want you can use "Tag" for that. Just mark your controls with tag "clear":

            foreach (Control c in Controls)
        {
            if (c.Tag == "clear")

            {
                c.Text = string.Empty;
            }
        }
litpost
  • 105
  • 3
  • 12
0

my code is old school with panels and controls placed in the panels. This is how I went about it WebUtil.ResetControls(PanelMD.Controls);

-1

Try this:

Form.Controls.OfType<Textbox>().ToList().ForEach(t => t.Text = "");
Nimantha
  • 6,405
  • 6
  • 28
  • 69
spyrax10
  • 5
  • 6
  • 1
    While this code may solve the question, [including an explanation](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion – Muhammad Dyas Yaskur May 21 '20 at 08:50