Clear multiple text boxes with a button in C#

2020-03-04 02:55发布

问题:

I use .NET framework 4.

In my form, I have 41 text boxes.

I have 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 clear with this code TextBoxName.Text = String.Empty; the textbox success cleared but one textbox, still had 40 textbox again.

How do I solve this?

EDIT

I put in this:

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 work.

Edit

I used panels and splitter.

回答1:

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

To use the above code, simply do this:

ClearAllText(this);


回答2:

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


回答3:

Step1: You need to go through all the Controls in the Form.

Step2: if a Control is TextBox then call Clear() function.

Try This:

        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 = "");
        }


回答4:

This works pretty well for myself.

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 simply call on it as you wish, for example, I am clearing all TextBoxes in a TabControl, which also includes the Tabs not on screen:

ClearTextBoxes(CustomerTabControl);