none
Is there a quick way to clear every control on a form? RRS feed

  • Question

  • Besides iterating thru each control and setting combo boxes = -1 and txt.clear() is there a simple method that you can call that would basically reload the form to its initial "after construction" phase? I have a form with controls on 4 different tabs and some code that takes place automatically upon form load that I would like to implement. I realize I can put the code I have on load into a method, call it on load, iterate and clear all controls then call the method but it would be much easier at this point to reload the form without the user seeing anything close or open.
    Thursday, December 22, 2011 7:54 PM

Answers

  • Hi,

    You can call the code below. It is pretty simple. After that you might want to call your load method too.

                this.Controls.Clear();
               InitializeComponent();
    


    Thursday, December 22, 2011 8:36 PM
  • If you mean by clearing controls to set the visible text to string.Empty (so there is no text inside a control), you cannot use the same property for textBox or comboBox or listView or listbox.

    Textbox has a Text property, comboBox has a Selectedindex.

    So to clear these different controls you cannot use only one propety for it.

    For textboxes you can do: textBox.Text =string.Empty (... = "")

    for comboBoxes: comboBox.SelectedIndex = -1;

    for listView or listBox: listBox.Clear();

     

    But if you have more same controls you can gather them like:

    TextBox[] tbs = { textBox1, textBox2, textBox3 };
    foreach(TextBox tb in tbs)
          tb.Text = string.Empty;
    

    The same you can do for other controls.


    Mitja
    Thursday, December 22, 2011 10:16 PM
  • Following Mitja's code, you can clear the similar controls like below,

    this.Controls.OfType<TextBox>().ToList().ForEach(txtBox => txtBox.ResetText());
    this.Controls.OfType<ComboBox>().ToList().ForEach(comboBox => { comboBox.ResetText(); comboBox.Items.Clear(); });
    this.Controls.OfType<ListBox>().ToList().ForEach(listBox => listBox.Items.Clear());
    this.Controls.OfType<ListView>().ToList().ForEach(listView => listView.Items.Clear());
    

     


    Please mark this post as answer if it solved your problem. Happy Programming!
    Friday, December 23, 2011 7:05 AM

All replies

  • Hi,

    You can call the code below. It is pretty simple. After that you might want to call your load method too.

                this.Controls.Clear();
               InitializeComponent();
    


    Thursday, December 22, 2011 8:36 PM
  • Possibly just open a new form and close the old one?

    You could copy over the data that you want to keep, if any, (i.e. the forms dimensions, position, etc. in the event that they moved it).

    Thursday, December 22, 2011 9:16 PM
  • If you mean by clearing controls to set the visible text to string.Empty (so there is no text inside a control), you cannot use the same property for textBox or comboBox or listView or listbox.

    Textbox has a Text property, comboBox has a Selectedindex.

    So to clear these different controls you cannot use only one propety for it.

    For textboxes you can do: textBox.Text =string.Empty (... = "")

    for comboBoxes: comboBox.SelectedIndex = -1;

    for listView or listBox: listBox.Clear();

     

    But if you have more same controls you can gather them like:

    TextBox[] tbs = { textBox1, textBox2, textBox3 };
    foreach(TextBox tb in tbs)
          tb.Text = string.Empty;
    

    The same you can do for other controls.


    Mitja
    Thursday, December 22, 2011 10:16 PM
  • Following Mitja's code, you can clear the similar controls like below,

    this.Controls.OfType<TextBox>().ToList().ForEach(txtBox => txtBox.ResetText());
    this.Controls.OfType<ComboBox>().ToList().ForEach(comboBox => { comboBox.ResetText(); comboBox.Items.Clear(); });
    this.Controls.OfType<ListBox>().ToList().ForEach(listBox => listBox.Items.Clear());
    this.Controls.OfType<ListView>().ToList().ForEach(listView => listView.Items.Clear());
    

     


    Please mark this post as answer if it solved your problem. Happy Programming!
    Friday, December 23, 2011 7:05 AM
  • this will do it.
    my vote up!
    • Edited by Ed Lu Friday, December 23, 2011 7:41 AM
    Friday, December 23, 2011 7:41 AM