Removing dynamic controls from panel

前端 未结 5 1440
既然无缘
既然无缘 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:41

    Since the controls are added at the run time, register them before using the findName method using the register method.

    StackPanel sp = new StackPanel
    {
       Name = "mySP",
       Orientation = Orientation.Horizontal,
    };
    
    //need to register the control to find it by name
    RegisterName(sp.Name, sp);
    
    //now to find control by name 
    StackPanel sp = (StackPanel)mainStackPanel.FindName("mySP");
    
    //deleting the control found
    mainStackPanel.Children.Remove(sp);
    
    //if you need to use the same name again, you have to unregister too
    UnregisterName(sp.Name);
    

提交回复
热议问题