C# - Clear all form fields at once

Asked

Viewed 74 times

-2

I have a form with several Textbox, Combobox, Radiobutton. And I would like to clear all fields at once. I’ve seen a lot of forums, but I haven’t found what could be my mistake. When I compile it does not return any error and runs normally and when I click to clear the fields I filled does not happen anything. I created the method in the class and in the Button Click event I called it.

Na I tried to put the method in the same place where the event is Click and instantiating so: ClearForm(this.Control); But nothing happens either.

Code in class:

    public class Interface
    {
        public void ClearForm(System.Windows.Forms.Control parent)
        {

            foreach (System.Windows.Forms.Control ctrControl in parent.Controls)
            {
                if (Object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.TextBox)))
                {
                    ((System.Windows.Forms.TextBox)ctrControl).Text = string.Empty;
                }

                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.ComboBox)))
                {
                    ((System.Windows.Forms.ComboBox)ctrControl).SelectedIndex = -1;
                }

                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RadioButton)))
                {
                    ((System.Windows.Forms.RadioButton)ctrControl).Checked = false;
                }
            }
        }
    }
}

In the Button Click event:

        private void btnClear_Click(object sender, EventArgs e)
        {
            Interface inter = new Interface();
            inter.ClearForm(this);
        }

1 answer

0


This is the method I use, is already recursive and will clear all screen controls:

public static void ClearControl(Control ctrl)
{
    foreach (Control c in ctrl.Controls)
    {
        if (c is TextBox)
        {
            ((TextBox)c).Text = "";
        }
        else if (c is RichTextBox)
        {
            ((RichTextBox)c).Text = "";
        }
        else if (c is ComboBox)
        {
            ((ComboBox)c).SelectedIndex = -1;
        }
        else if (c is CheckBox)
        {
            ((CheckBox)c).Checked = false;
        }
        else if (c is RadioButton)
        {
            ((RadioButton)c).Checked = false;
        }
        else if (c is DateTimePicker)
        {
            ((DateTimePicker)c).MinDate = new DateTime(1900, 1, 1);
            ((DateTimePicker)c).MaxDate = new DateTime(2100, 1, 1);
            ((DateTimePicker)c).Value = DateTime.Now.Date < ((DateTimePicker)c).MinDate ? ((DateTimePicker)c).MinDate : DateTime.Now.Date > ((DateTimePicker)c).MaxDate ? ((DateTimePicker)c).MaxDate : DateTime.Now.Date;
            if (((DateTimePicker)c).ShowCheckBox)
                ((DateTimePicker)c).Checked = false;
        }
        else if (c is NumericUpDown)
        {
            ((NumericUpDown)c).Value = 0 < ((NumericUpDown)c).Minimum ? ((NumericUpDown)c).Minimum : 0 > ((NumericUpDown)c).Maximum ? ((NumericUpDown)c).Maximum : 0;// ((NumericUpDown)c).Minimum;
        }
        else if (c is PictureBox)
        {
            ((PictureBox)c).Image = null;
        }
        else if (c is MaskedTextBox)
        {
            ((MaskedTextBox)c).Text = "";
        }
        else if (c is Label)
        {
            //((Label)c).Text = "";
        }
        else if (c is DataGridView)
        {
            ((DataGridView)c).DataSource = null;
        }
        else if (c is TrackBar)
            ((TrackBar)c).Value = ((TrackBar)c).Minimum;
        else if (c is RichTextBoxExtended)
            ((RichTextBoxExtended)c).ResetText();
        else if (c.HasChildren)
        {
            if (c is TabControl)
                ((TabControl)c).SelectedIndex = 0;

            ClearControl(c);
        }
    }
}

To call it, from within a Form, just do:

ClearControl(this);

I changed some things I use, which are specific to my environment, but you can change as you prefer.

  • 1

    I left only the codes for Textbox, Combobox and Radiobutton. And no error appeared, but when I click to delete it appears a message (I put Try Catch) Sorry error occurred: Input string was not in a correct format. And I click OK and it erases some items, but the message remains and every time I click OK, it deletes some items. Can you tell me why this is happening?

  • hello Cristina, just seeing the same exception, and on which line is happening

  • 1

    Hi!! I managed to find my mistake, thank you so much for the attention and attention!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.