Removing dynamic controls from panel

前端 未结 5 1444
既然无缘
既然无缘 2020-12-03 20:42

I have dynamically generated controls on the panels of windows form and i have also generated a button for removing the controls, all in rows.

int c = 0;
pri         


        
5条回答
  •  执念已碎
    2020-12-03 21:46

    If you have a Panel o other container with more dinamicaly components for remove all or more than one you can proceed in this way.

    In first step you load a component in array or list.

    List lst = new List();
    
    foreach (Object obj in this.PanelImplementationTeam.Controls)
    {     
        //DO NOT REMOVE COMPONENT IN THIS LOOP BECAUSE
        //YOU DECREASE THE .Controls  ARRAY AND YOU WILL HAVE AN ERROR      
        if (obj is PersonalUserControl)
        {
            lst.Add((PersonalUserControl)obj);
        }
    }
    
    foreach(PersonalUserControl uc in lst)
    {
        uc.Dispose();
    }
    

提交回复
热议问题