文件名称:添加动态控件.rar
文件大小:50KB
文件格式:RAR
更新时间:2023-04-10 05:49:12
c# 动态添加控件
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); } } }
【文件预览】:
添加动态控件
----添加动态控件.sln(1021B)
----.vs()
--------添加动态控件()
----添加动态控件()
--------Form1.cs(2KB)
--------bin()
--------obj()
--------Properties()
--------Program.cs(530B)
--------Form1.Designer.cs(3KB)
--------App.config(189B)
--------Form1.resx(6KB)
--------添加动态控件.csproj(4KB)