[英]How to delete range of controls in flowLayoutPanel c#
I am new to C# and I am using windows forms. 我是C#的新手,正在使用Windows窗体。
I have flowLayoutPanel
and I added 6 buttons
statically to it and then at the run-time I add some more buttons
. 我有
flowLayoutPanel
,并向其中静态添加了6个buttons
,然后在运行时添加了更多buttons
。
Now without clicking on any button
(buttons in flowLayoutPanel) I want to remove a range of buttons
(the ones which were created at run-time) based on their location / index. 现在,无需单击任何
button
(flowLayoutPanel中的按钮),我想根据其位置/索引删除一系列buttons
(在运行时创建的buttons
)。 For example: I already added 6 buttons
statically and then at run-time I added 10 more buttons
(16 buttons in total), now for example I want to remove buttons
between the 8th and 12th. 例如:我已经静态添加了6个
buttons
,然后在运行时又添加了10个buttons
(总共16个按钮),例如,现在我想删除8号和12号之间的buttons
。 I think there might be an index
for each control in the flowLayoutPanel
and if I know the index of the buttons
that would be helpful. 我认为
flowLayoutPanel
每个控件可能都有一个index
,如果我知道buttons
的索引会有所帮助。
private void RemoveSomeButtons_Click(object sender, EventArgs e)
{
//Remove buttons between the 8th and 12th
}
Anyone knows how to do that? 有人知道该怎么做吗?
Thank you 谢谢
You need to index these controls yourself, create a list to store your statically added buttons: 您需要自己索引这些控件,创建一个列表来存储您静态添加的按钮:
static List<Button> myButtonsList;
void InitializeButtons() {
for (int i = 0; i <= 6; i++) {
Button b = new Button();
flowLayout.Controls.Add(b);
myButtonsList.Add(b);
}
}
And then add each button created at runtime to that list, now you have an index, assigned to each button 然后将在运行时创建的每个按钮添加到该列表,现在您已经为每个按钮分配了一个索引
void CreateButtonAtRuntime() {
Button b = new Button();
flowLayout.Controls.Add(b);
myButtonsList.Add(b);
}
void DeleteButtons(int fromInd, int toInd) {
for (int i = toInd; i >= fromInd; i--) {
Button b = myButtonsList[i];
flowLayout.Controls.Remove(b);
myButtonsList.RemoveAt(i);
}
}
Remove controls based on their index 根据索引删除控件
private void RemoveControls(FlowLayoutPanel panel, int first, int last)
{
for (int i = first; i <= last; i++)
panel.Controls.RemoveAt(first);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.