winforms 从面板中删除动态控件

swvgeqrz  于 2023-08-07  发布在  其他
关注(0)|答案(6)|浏览(101)

我已经在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);
  }

字符串
现在,如何在单击某行中的按钮时删除该行中的所有控件?

mrwjdhj3

mrwjdhj31#

您仍然没有说明要删除哪个控件、要删除哪种类型的控件或要如何标识它们。
您可以循环遍历控件以删除特定的控件。
如果你有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;
        }
    }
}

weylhg0b

weylhg0b2#

如果您有一个面板或其他容器,其中包含更多的dinamicaly组件用于删除所有或多个,您可以以这种方式进行。
在第一步中,你在数组或列表中加载一个组件。

List<PersonalUserControl> lst = new List<PersonalUserControl>();

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();
}

字符串

owfi6suc

owfi6suc3#

如果要删除的所有控件都在面板中,则可以执行以下操作:panel.Controls.Clear();清除面板中的所有控件。

ryhaxcpt

ryhaxcpt4#

删除控件事件。From MSDN

以编程方式从集合中移除控件

1.从事件中移除事件处理程序。在Visual Basic中,使用RemoveHandler Statement关键字;在Visual C#中,使用-= Operator (C# Reference)
1.使用Remove方法从面板的Controls集合中删除所需控件。
1.调用Dispose方法释放控件使用的所有资源。
给出了使用VB和C#在链接页面中的具体例子。

jvidinwx

jvidinwx5#

由于控件是在运行时添加的,因此在使用register方法之前,请先注册它们。

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);

字符串

lskq00tm

lskq00tm6#

你可以通过Find方法和Remove方法来删除它。
1.首先,你必须为你生成的一组文本框或按钮(如相同的名称或东西)做一些独特的东西,之后你可以用这段代码点击“删除按钮”。在循环中运行它以在一个步骤中删除所有内容。

  • Controls.Remove(Controls.Find(“unique_Name”,true).LastOrDefault());*

我试着做了很多按钮,然后从最后一个按钮中删除了它。希望这对你有帮助。
2.别忘了从你的收藏中删除它,这可以通过找到并删除lastOne来完成。

  • yourCollection.RemoveAt(yourCollection.Count - 1);*

相关问题