Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 149,025 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,139 people online right now. Registration is fast and FREE... Join Now!





Clear a form

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

Submitted By: PsychoCoder
Actions:
Rating:
Views: 9,090

Language: C#

Last Modified: September 6, 2007
Instructions: Call the method like so: ClearForm(this);

Snippet


  1. public static void ClearForm(System.Windows.Forms.Control parent)
  2. {
  3.     foreach (System.Windows.Forms.Control ctrControl in parent.Controls)
  4.     {
  5.          //Loop through all controls
  6.          if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.TextBox)))
  7.          {
  8.               //Check to see if it's a textbox
  9.               ((System.Windows.Forms.TextBox)ctrControl).Text = string.Empty;
  10.                     //If it is then set the text to String.Empty (empty textbox)
  11.           }
  12.           else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RichTextBox)))
  13.           {
  14.                //If its a RichTextBox clear the text
  15.                ((System.Windows.Forms.RichTextBox)ctrControl).Text = string.Empty;
  16.           }
  17.           else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.ComboBox)))
  18.           {
  19.                //Next check if it's a dropdown list
  20.                ((System.Windows.Forms.ComboBox)ctrControl).SelectedIndex = -1;
  21.                     //If it is then set its SelectedIndex to 0
  22.           }
  23.           else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.CheckBox)))
  24.           {
  25.                //Next uncheck all checkboxes
  26.                ((System.Windows.Forms.CheckBox)ctrControl).Checked = false;
  27.           }
  28.           else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RadioButton)))
  29.           {
  30.                //Unselect all RadioButtons
  31.                ((System.Windows.Forms.RadioButton)ctrControl).Checked = false;
  32.           }
  33.           if (ctrControl.Controls.Count > 0)
  34.           {
  35.               //Call itself to get all other controls in other containers
  36.               ClearForm(ctrControl);
  37.           }
  38.      }
  39. }
  40.  
  41. //Example call
  42. ClearForm(this);

Copy & Paste


Comments


kamlakar 2008-04-14 05:02:26

marinaccio 2008-04-30 07:42:56

That's pretty clever!

wartech 2008-08-26 23:07:53

nice!


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month