C# Not Disposing controls like I told it to

后端 未结 4 1000
时光取名叫无心
时光取名叫无心 2021-01-26 21:34

I have a Panel control. And inside the panel users can add combobox\'s, textbox\'s labels etc and drag them around and stuff, and there\'s a Delete button on my form where if th

4条回答
  •  无人共我
    2021-01-26 21:54

    I know is a pretty old post, but I hope could help somebody.

    The only way I found that works is:

    while(panel.Controls.Count > 0)
    {
        panel.Controls[0].Dispose();
    }
    

    This way it doesn't matter if the list of Controls become smaller in the middle of the loop.

提交回复
热议问题