Remove all controls in a flowlayoutpanel in C#

前端 未结 4 1208
名媛妹妹
名媛妹妹 2021-02-10 01:45

I\'m building a flow layout panel whose each control represents for a room. I want to reload all room by removing all controls in the panel and adding new controls.

I us

4条回答
  •  天涯浪人
    2021-02-10 02:00

    That's because you are removing the controls from the same list you are iterating. Try something like this

    List listControls = flowLayoutPanel.Controls.ToList();
    
    foreach (Control control in listControls)
    {
        flowLayoutPanel.Controls.Remove(control);
        control.Dispose();
    }
    

    Maybe not like that, but you get the idea. Get them in a list, then remove them.

提交回复
热议问题