gpt4 book ai didi

c# - 从面板中删除动态控件

转载 作者:IT王子 更新时间:2023-10-29 04:53:20 26 4
gpt4 key购买 nike

我在 Windows 窗体的面板上动态生成了控件,并且还生成了一个用于删除控件的按钮,所有这些都是成行的。

int c = 0;
private void button1_Click(object sender, EventArgs e)
{
int v;
v = c++;
panel1.VerticalScroll.Value = VerticalScroll.Minimum;
ComboBox combo = new ComboBox();
combo.Name = "combobox" + v ;
combo.Location = new Point(30, 5 + (30 * v));

ComboBox combo2 = new ComboBox();
combo2.Name = "combobox2" + v ;
combo2.Location = new Point(170, 5 + (30 * v));

TextBox txt = new TextBox();
txt.Name = "txtbx" + v;
txt.Location = new Point(300, 5 + (30 * v));

TextBox txt2 = new TextBox();
txt2.Name = "txtbx2" + v;
txt2.Location = new Point(450, 5 + (30 * v));

TextBox txt3 = new TextBox();
txt3.Name = "txtbx3" + v;
txt3.Location = new Point(600, 5 + (30 * v));

Button btn = new Button();
btn.Name = "btn" + v;
btn.Text = "Remove";
btn.Location = new Point(750, 5 + (30 * v));
panel1.Controls.Add(combo);
panel1.Controls.Add(btn);
panel1.Controls.Add(txt);
panel1.Controls.Add(combo2);
panel1.Controls.Add(txt2);
panel1.Controls.Add(txt3);
btn.Click += new EventHandler(btn_Click);
combo.Tag = btn;
combo2.Tag = combo;
btn.Tag = combo2;

}

private void btn_Click(object sender, EventArgs e)
{
ComboBox cb3 = btnh.Tag as ComboBox;
ComboBox cb4 = cb3.Tag as ComboBox;
panel1.Controls.Remove(cb3);
panel1.Controls.Remove(cb4);
panel1.Controls.Remove(btnh);
}

现在如何在单击某行的按钮后删除该行的所有控件?

最佳答案

您仍然没有说明要删除哪个控件、要删除什么类型的控件或如何识别它们。

您可以循环遍历控件以删除特定控件。

如果您有 Linq,这很容易:

private void btn_Click(object sender, EventArgs e)
{
panel1.Controls.Clear(); //to remove all controls


//to remove all comboboxes
foreach (Control item in panel1.Controls.OfType<ComboBox>().ToList())
{
panel1.Controls.Remove(item);
}


//to remove control by Name
foreach (Control item in panel1.Controls.OfType<Control>().ToList())
{
if (item.Name == "bloodyControl")
panel1.Controls.Remove(item);
}


//to remove just one control, no Linq
foreach (Control item in panel1.Controls)
{
if (item.Name == "bloodyControl")
{
panel1.Controls.Remove(item);
break; //important step
}
}
}

编辑:

因为您已经标记了控件,所以做同样的事情很容易。您所需要的只是从标签中取回控件。 但你需要适本地标记:

改为这样做:

private void button1_Click(object sender, EventArgs e)
{
int v;
v = c++;
panel1.VerticalScroll.Value = VerticalScroll.Minimum;

Button btn = new Button();
btn.Name = "btn" + v;
btn.Text = "Remove";
btn.Location = new Point(750, 5 + (30 * v));
btn.Click += new EventHandler(btn_Click);

ComboBox combo = new ComboBox();
combo.Name = "combobox" + v ;
combo.Location = new Point(30, 5 + (30 * v));
combo.Tag = btn;

ComboBox combo2 = new ComboBox();
combo2.Name = "combobox2" + v ;
combo2.Location = new Point(170, 5 + (30 * v));
combo2.Tag = btn;

TextBox txt = new TextBox();
txt.Name = "txtbx" + v;
txt.Location = new Point(300, 5 + (30 * v));
txt.Tag = btn;

TextBox txt2 = new TextBox();
txt2.Name = "txtbx2" + v;
txt2.Location = new Point(450, 5 + (30 * v));
txt2.Tag = btn;

TextBox txt3 = new TextBox();
txt3.Name = "txtbx3" + v;
txt3.Location = new Point(600, 5 + (30 * v));
txt3.Tag = btn;

panel1.Controls.Add(combo);
panel1.Controls.Add(btn);
panel1.Controls.Add(txt);
panel1.Controls.Add(combo2);
panel1.Controls.Add(txt2);
panel1.Controls.Add(txt3);
}

private void btn_Click(object sender, EventArgs e)
{
//to remove control by Name
foreach (Control item in panel1.Controls.OfType<Control>().ToList())
{
if (item.Tag == sender || item == sender)
panel1.Controls.Remove(item);
}
}

在这里,您使用按钮标记控件,因此在单击按钮时,您可以删除所有控件,这些控件的标签是您从 sender 参数中获得的已单击按钮。但是这种方法的缺点是你必须枚举面板的所有控件,这不是很好。

编辑:据我了解,以下代码用于 OP 目前未使用的表格布局面板。但无论如何,表格面板布局更适合这项工作。

我建议你这样做:

private void button1_Click(object sender, EventArgs e)
{
int v;
v = c++;
panel1.VerticalScroll.Value = VerticalScroll.Minimum;

Button btn = new Button();
btn.Name = "btn" + v;
btn.Text = "Remove";
btn.Location = new Point(750, 5 + (30 * v));
btn.Click += new EventHandler(btn_Click);
btn.Tag = v;

ComboBox combo = new ComboBox();
combo.Name = "combobox" + v ;
combo.Location = new Point(30, 5 + (30 * v));
combo.Tag = v;

ComboBox combo2 = new ComboBox();
combo2.Name = "combobox2" + v ;
combo2.Location = new Point(170, 5 + (30 * v));
combo2.Tag = v;

TextBox txt = new TextBox();
txt.Name = "txtbx" + v;
txt.Location = new Point(300, 5 + (30 * v));
txt.Tag = v;

TextBox txt2 = new TextBox();
txt2.Name = "txtbx2" + v;
txt2.Location = new Point(450, 5 + (30 * v));
txt2.Tag = v;

TextBox txt3 = new TextBox();
txt3.Name = "txtbx3" + v;
txt3.Location = new Point(600, 5 + (30 * v));
txt3.Tag = v;

panel1.Controls.Add(combo);
panel1.Controls.Add(btn);
panel1.Controls.Add(txt);
panel1.Controls.Add(combo2);
panel1.Controls.Add(txt2);
panel1.Controls.Add(txt3);
}

private void btn_Click(object sender, EventArgs e)
{
int toBeDeletedRow = (int)((Control)sender).Tag;
for (int row = panel1.RowCount - 1; row >= 0; row--)
{
if (row == toBeDeletedRow)
{
panel1.RowStyles.RemoveAt(row);
panel1.RowCount--;
return;
}
}
}

关于c# - 从面板中删除动态控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13888558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com