基本信息
源码名称:winform 动态添加和删除控件 适合新手学习
源码大小:0.05M
文件格式:.rar
开发语言:C#
更新时间:2020-03-20
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

 public partial class Form1 : Form

    {
        public Form1()
        {
            InitializeComponent();
        }
        Label lab = new Label();//定义在到事件外面,这样多个按钮能公用该变量
        private void button1_Click(object sender, EventArgs e)
        {
            #region  //动态添加多个按钮
            for (int i=0;i<4;i )
            {
                Button btn = new Button();
                btn.Name = "MyButton" i;
                btn.Text = string.Format("按钮" i);
                btn.Size = new Size(72, 48);
                btn.Location = new Point(200, 180 70*i);
                this.Controls.Add(btn);
            }
            #endregion
            #region//动态添加标签
          
            lab.Name = "MyLabel";
            lab.Text = string.Format("标签");
            lab.Size = new Size(72, 48);
            lab.Location = new Point(100, 180);
            this.Controls.Add(lab);
            #endregion
            #region//动态添加文本框
            TextBox tb = new TextBox();
            tb.Name = "MyTextBox";
            tb.Text = string.Format("文本框");
            tb.Size = new Size(72, 48);
            tb.Location = new Point(300, 180);
            this.Controls.Add(tb);
            #endregion

            #region//往容器内添加标签
            Label lab1 = new Label();
            lab1.Name = "MyLabel1";
            lab1.Text = string.Format("标签");
            lab1.Size = new Size(72, 48);
            lab1.Location = new Point(710, 180);
            panel1.AutoScroll = true;  //为panel添加滚动条
            panel1.Controls.Add(lab1);//为panel添加控件
            panel1.BorderStyle = BorderStyle.FixedSingle;//为panel添加边框
           // panel1.BorderStyle = BorderStyle.None;//为panel取消边框
            #endregion



        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Controls.Remove(lab);
        }
    }
}