AMH
AMH

Reputation: 6451

Clear controls Dynamically

I have groupbox I want to clear all the control in it , I try

public void ClearPanels(GroupBox control)
{

    foreach (Control p in control.Controls)
    {

        control.Controls.Remove(p);


    }

}

but a panel remain it , the problem I create the controls in runtime , and want to remove it in runtime

Upvotes: 1

Views: 1117

Answers (2)

amit_g
amit_g

Reputation: 31250

Use RemoteAt

while (control.Controls.Count > 0)
{
    control.Controls.RemoveAt(0);
}

or Clear

control.Controls.Clear();

Upvotes: 2

Teoman Soygul
Teoman Soygul

Reputation: 25732

Better use this which clears all the controls at once without using a loop:

public void ClearPanels(GroupBox control)
{
  control.Controls.Clear();
}

Upvotes: 3

Related Questions