public partial class Form1 : Form
{
private System.Windows.Forms.Button button1=new Button ();
public Form1()
{
InitializeComponent();
this.Controls.Add(button1);
this.button1.Click += new System.EventHandler(this.button1_Click);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("hello world!");
}
}
顺便说一下,在你拖动控件的时候,窗体设计器自动为你生成了上述代码,在desiger.cs里面。
用委托绑定一个方法.
btnExp1.Click+=btnDF_Click;
//预定义的方法
private void btnDF_Click(object sender, EventArgs e){...}
爱仕达