C#用代码创建的控件怎么用,比如我用代码创建一个按钮,那么怎么使用单击这个按钮的事件呢,怎么写

如题所述

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里面。
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-09-13
用委托绑定一个方法.
btnExp1.Click+=btnDF_Click;

//预定义的方法
private void btnDF_Click(object sender, EventArgs e){...}本回答被网友采纳
第2个回答  2012-09-13
爱仕达
相似回答