C#动态创建控件并绑定事件
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
Button btn = new Button();
btn.Size = new Size(100, 25);
btn.Text = (i + 1).ToString();
btn.Location = new Point(i*120,12);
btn.Click += new EventHandler(Btn_Click);
btn.MouseClick += new MouseEventHandler(Btn_MouseClick);
this.Controls.Add(btn);
}
}
public void Btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
MessageBox.Show(btn.Text);
}
public void Btn_MouseClick(object sender, MouseEventArgs e)
{
Button btn = (Button)sender;
if (e.Button == MouseButtons.Right)
{
}
}