Clear multiple text fields with a button in C #

I am using the .NET framework 4.

In my form, I have 41 text boxes.

I tried with this code:

private void ClearTextBoxes()
        {
            Action<Control.ControlCollection> func = null;

            func = (controls) =>
            {
                foreach (Control control in controls)
                    if (control is TextBox)
                        (control as TextBox).Clear();
                    else
                        func(control.Controls);
            };

            func(Controls);
        }

And this code:

private void ClearTextBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null)
                    tb.Text = String.Empty;
                else
                    ClearTextBoxes(ctrl.Controls);
            }
        }

This still does not work for me.

When I tried to clean up with this code TextBoxName.Text = String.Empty;, the success of the text field was deleted, but one text field still contained 40 text fields.

How to solve this?

EDIT

I added the following:

private void btnClear_Click(object sender, EventArgs e)
        {

            ClearAllText(this);

        }

void ClearAllText(Control con)
        {
            foreach (Control c in con.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Clear();
                else
                    ClearAllText(c);
            }
        }

but still not working.

Edit

Image

I used panels and a separator.

+4
source share
4 answers
void ClearAllText(Control con)
{
    foreach (Control c in con.Controls)
    {
      if (c is TextBox)
         ((TextBox)c).Clear();
      else
         ClearAllText(c);
    }
}

To use the code above, simply do the following:

ClearAllText(this);
+14
source

You tried

 private void RecursiveClearTextBoxes(Control.ControlCollection cc)
 {
   foreach (Control ctrl in cc)
   {
     TextBox tb = ctrl as TextBox;
     if (tb != null)
     tb.Clear();

    else
    RecursiveClearTextBoxes(ctrl.Controls);
   }
+1
source

1: Controls Form.

2:, a Control - TextBox, Clear().

:

        void clearText(Control control)
        {
            foreach (Control c in control.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Clear();
                else
                    clearText(c);
            }
        }
        public void ModifyControl<T>(Control root, Action<T> action) where T : Control
        {
            if (root is T)
                action((T)root);
            // Call ModifyControl on all child controls
            foreach (Control control in root.Controls)
                ModifyControl<T>(control, action);
        }
        private void button5_Click(object sender, System.EventArgs e)
        {
           clearText(this);
           ModifyControl<TextBox>(splitContainer1, tb => tb.Text = "");
        }
0
source

This is very good for me.

void ClearTextBoxes(DependencyObject dObject)
{
    TextBox tb = dObject as TextBox;
    if (tb != null)
        tb.Text = null;

    foreach (DependencyObject obj in dObject.GetChildObjects())
        ClearTextBoxes(obj);
}

And then just call it as you wish, for example, I clear all the text fields in TabControl, which also includes tabs that are not displayed on the screen:

ClearTextBoxes(CustomerTabControl);
0
source

All Articles