C#新增按钮

代码亲测可用,似乎不需要“ADD”,如下:
form_load段:
for (int i = 0; i < 10; i++)
{
btn = new Button();
btn.Parent = this.panel1;
//btn.Top = 25 * i;
btn.Name = "btn" + i.ToString();
btn.Text = "btn" + i.ToString();
btn.Click += new EventHandler(btn_Click);
}
btn_Click段:
void btn_Click(object sender, EventArgs e)
{
//MessageBox.Show("" + ((Button)sender).Name);
string name = ((Button)sender).Name;
switch (name)
{
case "btn0":
//btn0处理代码
break;
case "btn1":
//处理代码
break;
case "btn2":
//处理代码
break;
//继续添加
}
}
posted @ 2019-05-01 10:03  厦门哈韩  阅读(420)  评论(0编辑  收藏  举报