locked
To clear all controls RRS feed

  • Question

  • hello,

    In my Form I had used multiple container such as panel,group box.and as per my requirement i have to clear all the controls which are reside in container. For this i have wrote a code as follows:- 

    foreach (Control x in ControlContainer.Controls)//Error { if (x is System.Windows.Forms.TextBox) ((TextBox)x).Text = String.Empty; else (x is System.Windows.Forms.ComboBox)

    { ((ComboBox)x).SelectedIndex = -1; ((TextBox)x).Text = String.Empty; }

     

    But its not working properly, i'm receiving error like:'object' does not contain a definition for 'Controls'

    please help

    Monday, April 18, 2011 7:25 PM

Answers

  • Hi,

    The code you have does not indicate where the error may be.  It is generally not a good idea to simply clear all controls directly.  If you use DataBinding, clearing controls is pretty much a matter of just resetting the DataBinding or the properties on the bound object.

    However, you can recursively traverse all the controls and clear them according to whatever rules you want.

    void ClearControls()
    {
      foreach (Control c in this.Controls)
      {
        if (c is ContainerControl)
          ClearControl(c);
      }
    }
    
    void ClearControl(Control c)
    {
      c.Text = string.Empty;
    
      if(c is ComboBox)
      {
        ((ComboBox) c).SelectedIndex = -1;
        ((ComboBox) c).Items.Clear();
      }
      //etc
    
      foreach (Control child in c.Controls)
        ClearControl(child);
    }
    

     


    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    • Marked as answer by prachi patil Tuesday, April 19, 2011 4:38 PM
    Tuesday, April 19, 2011 5:56 AM

All replies

  • Hi,

    The code you have does not indicate where the error may be.  It is generally not a good idea to simply clear all controls directly.  If you use DataBinding, clearing controls is pretty much a matter of just resetting the DataBinding or the properties on the bound object.

    However, you can recursively traverse all the controls and clear them according to whatever rules you want.

    void ClearControls()
    {
      foreach (Control c in this.Controls)
      {
        if (c is ContainerControl)
          ClearControl(c);
      }
    }
    
    void ClearControl(Control c)
    {
      c.Text = string.Empty;
    
      if(c is ComboBox)
      {
        ((ComboBox) c).SelectedIndex = -1;
        ((ComboBox) c).Items.Clear();
      }
      //etc
    
      foreach (Control child in c.Controls)
        ClearControl(child);
    }
    

     


    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    • Marked as answer by prachi patil Tuesday, April 19, 2011 4:38 PM
    Tuesday, April 19, 2011 5:56 AM
  • just call Clear method of the Controls property. it will clear all controls

     

    Tuesday, April 19, 2011 6:22 AM
  • just call Clear method of the Controls property. it will clear all controls

     


    That will REMOVE the controls from the Controls collection, but it may be what the OP wants.
    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    Tuesday, April 19, 2011 7:16 AM
  • Tuesday, April 19, 2011 12:43 PM
  • Hey thanx For your help .it works

    ---
    Happy Coding!!!

    Tuesday, April 19, 2011 4:43 PM