7 Replies - 2232 Views - Last Post: 27 June 2012 - 07:22 AM

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Clear a form

Posted 06 September 2007 - 05:43 PM

Description: Call the method like so: ClearForm(this);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);


Is This A Good Question/Topic? 0
  • +

Replies To: Clear a form

#2 marinaccio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 09-April 08

Re: Clear a form

Posted 30 April 2008 - 04:42 AM

That's pretty clever!
Was This Post Helpful? 0
  • +
  • -

#3 wartech  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: Clear a form

Posted 26 August 2008 - 08:07 PM

nice!
Was This Post Helpful? 0
  • +
  • -

#4 wjstarck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 20-January 10

Re: Clear a form

Posted 20 January 2010 - 12:13 PM

Nice snippet....saved my lots of hair pulling. Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 wjstarck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 20-January 10

Re: Clear a form

Posted 20 January 2010 - 06:02 PM

Arrgh...unfortunately it doesn't work if the controls are contained in a group box. Any suggestions? Thx
Was This Post Helpful? 0
  • +
  • -

#6 wjstarck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 20-January 10

Re: Clear a form

Posted 20 January 2010 - 07:01 PM

Gaah..nvm. I was trying inside of a mousedown event instead of a listbox_SelectedIndexChanged. Works like a charm now. Thanks a million...
Was This Post Helpful? 0
  • +
  • -

#7 UnderCeej  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 31-May 12

Re: Clear a form

Posted 31 May 2012 - 05:06 AM

Excellent, works brilliantly :) Thanks!
Was This Post Helpful? 0
  • +
  • -

#8 RedYote  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 17-May 12

Re: Clear a form

Posted 27 June 2012 - 07:22 AM

Curious; if I wished to use this to clear an entire form, how would I use it? Perhaps I'm simply not seeing how to call it properly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1