Remove all controls in a flowlayoutpanel in C#

前端 未结 4 1262
名媛妹妹
名媛妹妹 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:04

    Note: this is a working solution based on the previous comment, so credits to that person :)

    This worked for me:

    List listControls = new List();
    
    foreach (Control control in flowLayoutPanel1.Controls)
    {
         listControls.Add(control);
    }
    
    foreach (Control control in listControls)
    {
         flowLayoutPanel1.Controls.Remove(control);
         control.Dispose();
    }
    

    There is probably a better/cleaner way of doing it, but it works.

提交回复
热议问题