C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C# Source Code


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

Join 340,144 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 3,815 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: 18,158

Language: C#

Last Modified: September 7, 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!

clarence_cool03 2009-05-13 22:48:33

{-='coOL prOgraM... 'gUy'z can u heLp me hOw tO prOgRam a supermarket or grocery prOgram??... 'pLs gUY'z i jUz neEd 8 2mORrOw... 'pLs jUz email 8 2 my email aDD... 'clarence_cool03@y.c... 'pLs aSap... 'pLs... =-}

Rathika 2009-08-11 02:46:07

Hi How to Clear a Web Page Control values

wjstarck 2010-01-20 14:13:17

Nice snippet....saved my lots of hair pulling. Thanks!

wjstarck 2010-01-20 20:02:46

Arrgh...unfortunately it doesn't work if the controls are contained in a group box. Any suggestions? Thx

wjstarck 2010-01-20 21:01:25

Gaah..nvm. I was trying inside of a mousedown event instead of a listbox_SelectedIndexChanged. Works like a charm now. Thanks a million...


Add comment


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





Live C# Help!

Be Social

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

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month