Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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

[edit]Inline code converted to code block - OriginalGriff[/edit]
Posted
Updated 18-Apr-11 9:11am
v2
Comments
Henry Minute 18-Apr-11 16:48pm    
From ricmil42 - posted in the wrong place (see below)

"Original Poster - you may want to look at the "Winform TextBox Properties" question. Specifically the recursive answer. It can be modified to clear any control as well as controls that contain controls (panel, group box, etc.)."
Sander Rossel 18-Apr-11 17:02pm    
I was thinking the same thing :)
http://www.codeproject.com/Answers/183503/Winform-TextBox-Properties.aspx?cmt=86720#answer4

That makes sense , depending on what kind of object you are using. If ControlContainer is not an instance of an object that derives from Control, then it will not contain a Controls list. ControlContainer sounds like a class, rather than an instance?
Try:
GroupBox gb = new GroupBox();
...
foreach (Control x in gb)
   {
   ...
   }
 
Share this answer
 
Comments
ricmil42 18-Apr-11 16:36pm    
Original Poster - you may want to look at the "Winform TextBox Properties" question. Specifically the recursive answer. It can be modified to clear any control as well as controls that contain controls (panel, group box, etc.).
Sergey Alexandrovich Kryukov 18-Apr-11 21:51pm    
You cover only one level (my vote of 5 anyway), but this code can be universal at practically the same price, please see my recursive solution.
--SA
You need to use recursion in this case:

static void CleanupTextBox(TextBox textBox) {
   //whatever cleanup you need
}
static void CleanupComboBox(ComboBox textBox) {
   //whatever cleanup you need
}
static void CleanupBoxes(Control parent) {
   TextBox textBox = parent as TextBox;
   ComboBox comboBox = parent as ComboBox;
   if (textBox != null)
      CleanupTextBox(textBox);
   else if (comboBox != null)
      CleanupComboBox(comboBox);
   foreach(Control control in parent.Controls)
      SetupTextBoxes(control);
}

//...

CleanupBoxes(myFormInstance);
or, in the method of your form:
CleanupBoxes(this);


—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900