Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 95,475 C# Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 976 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C# Expert

Register to Make This Box Go Away!



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: 5,117

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!


Add comment


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





Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->