Saturday 1 October 2011

Reset Form Fields to default NULL Values

Question Received:
         How to reset form fields and controls thru programming?

Solution: 
         To reset the form controls and fields with NULL (or) Empty String,
          Call function ResetFields(this) from any function,
              public static void ResetFields(Control form)
             {
                   foreach (Control ctrl in form.Controls)
                  {
                       if (ctrl.Controls.Count > 0)
                       ResetFields(ctrl);
                       Reset(ctrl);
                  }
             }
             public static void Reset(Control ctrl)
            {
                  if (ctrl is TextBox)
                  {
                       TextBox tb = (TextBox)ctrl;
                       if (tb != null)
                      {
                           tb.ResetText();
                      }
                  }
                  else if (ctrl is ComboBox)
                  {
                       ComboBox cb = (ComboBox)ctrl;
                       if (cb != null)
                      {
                           cb.ResetText();
                      }
                  }
                  else if (ctrl is CheckBox)
                  {
                       CheckBox chk = (CheckBox)ctrl;
                       if (chk != null)
                       {
                           chk.Checked = false;
                       }
                  }
                  else if (ctrl is RadioButton)
                  {
                       RadioButton rb = (RadioButton)ctrl;
                       if (rb != null)
                       {
                           rb.Enabled = true;
                       }
                  }
             }


No comments:

Post a Comment