954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Clear a form

By avirag on Jan 19th, 2010 12:29 pm

This is a snippet I use to clear a form. Right now it clears: TextBox, RichTextBox, ComboBox, CheckBox and RadioButton. More can be added if needed...
:)

public static void ClearForm(System.Windows.Forms.Control parent)
{
    foreach (System.Windows.Forms.Control ctrControl in parent.Controls)
    {
         //Loop through all controls 
         if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.TextBox)))
         {
              //Check to see if it's a textbox 
              ((System.Windows.Forms.TextBox)ctrControl).Text = string.Empty;
                    //If it is then set the text to String.Empty (empty textbox) 
          }
          else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RichTextBox)))
          {
               //If its a RichTextBox clear the text
               ((System.Windows.Forms.RichTextBox)ctrControl).Text = string.Empty;
          }
          else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.ComboBox)))
          {
               //Next check if it's a dropdown list 
               ((System.Windows.Forms.ComboBox)ctrControl).SelectedIndex = -1;
                    //If it is then set its SelectedIndex to 0 
          }
          else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.CheckBox)))
          {
               //Next uncheck all checkboxes
               ((System.Windows.Forms.CheckBox)ctrControl).Checked = false;
          }
          else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RadioButton)))
          {
               //Unselect all RadioButtons
               ((System.Windows.Forms.RadioButton)ctrControl).Checked = false;
          }
          if (ctrControl.Controls.Count > 0)
          {
              //Call itself to get all other controls in other containers 
              ClearForm(ctrControl);
          }
     }
}

//Example call
ClearForm(this);
# if (ctrControl.Controls.Count > 0) # { # //Call itself to get all other controls in other containers # ClearForm(ctrControl); # }

That's clever. Gets all the child controls.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

Agreed, the recursive call is a nice touch...good job :)

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

Nice snippet. I've actually been using something along these lines for some web apps I've been working on.

I do have a question though: Which is faster or more "correct"?

if(object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.ComboBox)))


or

if(ctrControl is System.Windows.Forms.ComboBox)


Is either of those better than the other, or they used for entirely different things? I ask because I'm using the latter, and if the former is a better or more proper use, I'd rather switch to that one.

Sodabread
Posting Whiz in Training
290 posts since Nov 2009
Reputation Points: 103
Solved Threads: 42
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: