动态创建控件及操作控件

来源:互联网 发布:简单的编程代码 编辑:程序博客网 时间:2024/05/17 11:55

动态创建控件,并附加上Click控件‍

 

‍Button button = new Button();

button.Name = "btn" + “1”;
button.Text = "开始";
button.Size = new Size(75, 23);
this.Controls.Add(button);
button.Click += new EventHandler(btn_Click);

 

 

‍添加Click事件

 

private void btn_Click(object sender, System.EventArgs e)

{

         事件代码

}

 

 

动态操作控件

 

System.Windows.Forms.Control constrol = panel_full.Controls["btn" + (n).ToString()];
if (constrol != null)
{
        Button button = (Button)constrol;
        button.Location = new Point(3, 107);

}