删除动态控件 [英] Removing dynamic controls

查看:62
本文介绍了删除动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在面板上动态生成控件,我还生成了一个用于删除控件的按钮,

控件在行代码上,





I have dynamically generated controls on the panels and i have also generated a button for removing the controls,
controls are on a line code is,


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);
          
        }
        private void btn_Click(object sender, EventArgs e)
        {
            
// what i have to write here for removing only the textbox and combobox and  button itself to be removed only the controls which are  aside the button

        }





我有什么写入按钮单击事件只删除文本框和组合框和按钮本身被删除按钮旁边的其他行控件不应该受到影响,



what i have to write in the button click event for removing only the textbox and combobox and button itself to be removed the controls which are aside the button other line controls should not be effected by that,

推荐答案

我认为现在是时候向您介绍一个新想法了。为什么不把你的三个控件当作一个控件呢?



有一种类可以添加到你的项目中,叫做用户控件 [ ^ ] - 它是你用控件填充的容器,并添加你的代码以使它们一起工作。然后,当你想要添加所有控件时,你可以创建一个用户控件的实例,并添加它 - 它会处理你想要绘制的三个控件。



我会创建一个,在设计时添加你的文本框,组合框和按钮,并提供一个父项处理的事件,以便在请求时将其删除。这样,删除代码只需要访问你的Panel.Controls列表,并使用带有 sender 参数的Remove方法一次性取出所有三个...



(它真的很容易 - 但它会使你的代码变得更加简单!)
I think it''s time to introduce you to a new idea. Why not treat your three controls as a single control?

There is a type of class you can add to your project called a UserControl[^] - it''s a container that you fill with your controls, and add your code to make them work together. Then, when you want to add all of the controls, you create an instance of your user control instead, and add that - it handles adding the three controls you want to draw.

I would create one, add your textbox, combobox and button at design time, and provide an event which the parent handles to remove it when requested. That way, the remove code just needs to access your Panel.Controls list, and use the Remove method with the sender parameter to take all three out at once...

(And it''s really easy to do - but it makes your code a lot, lot simpler!)


不是我明确要求删除控制权吗?面板中的每个位置?



此代码删除控制文本和组合(包括按钮)

is not clear to me for remove control in line? every location in a panel?

this code remove controls texts and combos (include button)
private void button2_Click(object sender, EventArgs e)
 {
     panel1.Controls.OfType<TextBox>().ToList().ForEach(t => panel1.Controls.Remove(t));
     panel1.Controls.OfType<ComboBox>().ToList().ForEach(t => panel1.Controls.Remove(t));

 }





我用Windows窗体证明它,包括面板和你要添加的控件是递增一行



I prove it with a windows form including the panel and the control that you are adding is incrementing a line


foreach(控制c在panel1.Controls中)

{

if(c is TextBox)

{

panel1.Controls.Remove(c);

}

if(c是ComboBox)

{

panel1.Controls.Remove(c);

}



}



也许这个?在你的活动中点击按钮??
foreach (Control c in panel1.Controls)
{
if (c is TextBox)
{
panel1.Controls.Remove(c);
}
if (c is ComboBox)
{
panel1.Controls.Remove(c);
}

}

maybe this? in your event click button??


这篇关于删除动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆