动态添加控件及删除

方法1:

        int i = 0;
        List<PhoneTextBox> list = new List<PhoneTextBox>();      
        private void btnAdd_Click(object sender, EventArgs e)
        {           
            if (i >= 9)//只能添加9个(看自己需求)
            {
                return;
            }
            else
            {
                i = i + 1;
                PhoneTextBox txtPhone = new PhoneTextBox();


                txtPhone.Name = "txtPhone" + i;


                txtPhone.Location = new Point(textBox4.Location.X, textBox4.Location.Y + textBox4.Height + txtPhone.Height * (i - 1) + 4);


                txtPhone.BTNdel.Click += txtPhone_Click;              
               
                panel5.Controls.Add(txtPhone);


                panel6.Location = new Point(panel6.Location.X, panel6.Location.Y + txtPhone.Height);
                int n = txtPhone.Height;
                list.Add(txtPhone);
                //保证复制后的控件都在原控件下方显示
                txtPhone.BringToFront();
            }         
        }
      
        private void txtPhone_Click(object sender, EventArgs e)
        {
            Control c = (Control)sender; 
            string name = c.Parent.Name;

            int nIndex = GetControlsName(c.Parent.Name);
          
            for (int n = nIndex + 1; n < list.Count; n++)
            {               
                  list[n].Location = new Point(list[n].Location.X, list[n].Location.Y - list[n].Height);
            }
            list.RemoveAt(nIndex);
            this.panel6.Location = new Point(this.panel6.Location.X, this.panel6.Location.Y - c.Height);
            int b = c.Height;
            panel5.Controls.Remove(c.Parent);
            BindClick(c);
            i = i - 1;
        }
        //根据控件名称找控件索引
        public int GetControlsName(string name)
        {
            int nIndex = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Name == name)
                {
                    nIndex = i;
                    break;
                }
            }
            return nIndex;
        }


        //绑定删除控件事件的代码
        private void BindClick(Control cl)
        {
            //保证如何在删除和添加时能释放资源(即可以紧跟在显示的控件后面显示)
            cl.Controls.Clear();
            DisposeControls(cl);
            //释放资源
            cl.Dispose();
        }
        //在清空控件时如何释放资源
        private void DisposeControls(Control cParent)
        {
            foreach (Control c in cParent.Controls)
            {
                //DisposeControls(c);
                c.Controls.Clear();
                c.Dispose();
            }

        }

设计视图和运行结果如图

                                                                                                     

方法2:

在窗体加一个flowLayoutPanel1控件还有一个button按钮

  private void btnadd_Click(object sender, EventArgs e)
        {
            string controlMark = Guid.NewGuid().ToString();
            TextBox txt1 = new TextBox();
            txt1.Name = "txt_" + controlMark;          
            txt1.Width = 120;
            flowLayoutPanel1.Controls.Add(txt1);


            Button btnDel = new Button();
            btnDel.Name = "del_" + controlMark;
            btnDel.Text = "删除";
            btnDel.Click += new EventHandler(delControl);
            flowLayoutPanel1.Controls.Add(btnDel);
        }


        ///点击删除按钮删除生成的控件
        public void delControl(object sender, EventArgs e)
        {
            Button btnAction = sender as Button;
            string id = btnAction.Name.Split('_')[1];
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c.Name == "txt_" + id)
                {
                    flowLayoutPanel1.Controls.Remove(c);
                }
            }
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c.Name == "del_" + id)
                {
                    flowLayoutPanel1.Controls.Remove(c);
                }
            }
           
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值