1
Answer

How to clear control that has children?

Sai Yang

Sai Yang

4y
858
1
hi there!
 
I want to clear control in child control. I use twice foreach loop, but the scond loop doesn't support with error like.
 
Error 1 foreach statement cannot operate on variables of type 'System.Windows.Forms.Control' because  'System.Windows.Forms.Control' does not contain a public definition for 'GetEnumerator'
F:\Programs\C#\AUTOSERVICE\AUTOSERVICE\Home.cs 141 25 AUTOSERVICE
 
I tried the code:
 
foreach (Control control in pnHome.Controls)
{
    if (control is UserControl) // This control shown into a panel I give you code bellow. (This control is pnHome bellow)
    {
        if (control.HasChildren)
        {
            foreach (Control c in control) // this statement error as above
            {
                //I want to clear all control under myControl (See code bellow). (TextBox, CheckBox, Label, .. .etc )
            }
        }
    }
 
Code to show UserControl to a Panel
             
myControl  u = new myControl();
u.Dock = DockStyle.Fill;
pnHome.Controls.Clear();
pnHome.Controls.Add(u); 
 
I don't know why?
 
Please, share help me to clear the control.
 
Thank you very much.
 
Answers (1)