Pages

Sunday, September 6, 2009

Clear all controls on a Form / Panel / GroupBox etc c# by Praveen P.R

 //Clear All values in the Form / Panel / GroupBox  etc
  public static void clearControls(Control crl)
  {
  foreach (Control c in crl.Controls)
  {
  if (c.GetType().Name == "GroupBox" || c.GetType().Name == "CustomGroupBox")
  {
  clearControls(c);
  }
  else if (c.GetType().Name == "TextBox")
  {
  ((TextBox)c).Text = "";
  }
  else if (c.GetType().Name == "CustomTextBox")
  {
  ((CustomControls.CustomTextBox)c).Text = "";
  }
  else if (c.GetType().Name == "ComboBox")
  {
  if (((ComboBox)c).Items.Count > 0)
  {
  ((ComboBox)c).SelectedIndex = 0;
  }
  }
  }
  }
//To call the above funtion

clearControls(this);
or
clearControls(panel); // pass panel Object

or
clearControls(Groupbax); // pass groupbax Object

No comments: