C#windows窗体应用程序中怎么 一次性添加多个按钮,最好用数组,请大神解决
2个回答
展开全部
namespace AddBtns
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Button[] btn = new Button[5];
private void OnClick(object sender, EventArgs e)
{
MessageBox.Show(((Button)sender).Name);
}
private void Form1_Load(object sender, EventArgs e)
{
int w = 200;
int h = 30;
for (int i = 0; i < 5; i++)
{
btn[i] = new Button();
btn[i].Name = "btn" + i.ToString();
btn[i].Text = "btn" + i.ToString();
btn[i].Width = w;
btn[i].Height = h;
btn[i].Left = 30;
btn[i].Top = (h + 10) * i;
this.Controls.Add(btn[i]);
btn[i].Click += new EventHandler(OnClick);
}
}
}
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Button[] btn = new Button[5];
private void OnClick(object sender, EventArgs e)
{
MessageBox.Show(((Button)sender).Name);
}
private void Form1_Load(object sender, EventArgs e)
{
int w = 200;
int h = 30;
for (int i = 0; i < 5; i++)
{
btn[i] = new Button();
btn[i].Name = "btn" + i.ToString();
btn[i].Text = "btn" + i.ToString();
btn[i].Width = w;
btn[i].Height = h;
btn[i].Left = 30;
btn[i].Top = (h + 10) * i;
this.Controls.Add(btn[i]);
btn[i].Click += new EventHandler(OnClick);
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询