动态生成控件的响应事件问题 button2.Click += new System.EventHandler(this.button2_Click);private void button1_Click(object sender, System.EventArgs e){ ...} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 上面错误button2.Click += new System.EventHandler(this.button2_Click);private void button2_Click(object sender, System.EventArgs e){ ...} to :HNU 你好,多谢你回答我的问题,我意思是按button1后新建一个button2.那么怎样给它加事件响应如下:private void button1_Click(object sender, System.EventArgs e){System.Windows.Forms.Button button2 = new System.Windows.Forms.Button();button2.Location = new System.Drawing.Point(120, 100);button2.Name = "button2";button2.Size = new System.Drawing.Size(80, 24);button2.TabIndex = 0;button2.Text = "button2";this.Controls.Add(button2);(至此已经新建一个按钮了。但怎样加入事件响应)..}响应函数应该怎样加入。你说的方法我之前已经试过,button2.Click += new System.EventHandler(this.button2_Click);但是是没有this.button2_Click这个方法的。因为根本还没有定义。应定义在那里。 就定义在你的Form类下面private void button2_Click(object sender, System.EventArgs e){ ...} 你只要打入:button2.Click += 注意到这儿时,它会提示你按Tab键的,那你就按吧~~好了,完工 private void btnTest1_Click(object sender, System.EventArgs e){ Button btnTest2 = new Button(); btnTest2.Name = "btnTest2"; btnTest2.Text = "按钮2"; btnTest2.Size = btnTest1.Size; btnTest2.Location = new Point(btnTest1.Location.X + btnTest1.Size.Width + 5, btnTest1.Location.Y); btnTest2.Click+=new EventHandler(btnTest2_Click); this.Controls.Add(btnTest2);}private void btnTest2_Click(object sender, EventArgs e){ MessageBox.Show("Hello,World");} 响应函数应该怎样加入。你说的方法我之前已经试过,button2.Click += new System.EventHandler(this.button2_Click);但是是没有this.button2_Click这个方法的。因为根本还没有定义。应定义在那里。========》再类里面定义就行了,其实手动添加事件设计器会提示你按tab键生成方法的。 C# 重复打开多个程序 DataGridView中的单元格转化成Dropdownlist形式 求救:字体和字库 请教:如何确保同时只能开一个开发的软件? 请问以前编程序常用的key属性用什么代替了? c#、存储过程的参数问题 propertyGrid又一疑问: 如何设定属性组(category)的排列顺序? 什么是 oa ???? c#中如何把HTML文档转换成XML文档 将webform中控件的引用传给类库会不会不安全。有问题吗。比如HtmlInputFile传到类库中操作 求教,一个关于c#中连接Access数据库的问题! replace语句她就是不工作!郁闷!
上面错误
button2.Click += new System.EventHandler(this.button2_Click);
private void button2_Click(object sender, System.EventArgs e)
{
...}
private void button1_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.Button button2 = new System.Windows.Forms.Button();
button2.Location = new System.Drawing.Point(120, 100);
button2.Name = "button2";
button2.Size = new System.Drawing.Size(80, 24);
button2.TabIndex = 0;
button2.Text = "button2";
this.Controls.Add(button2);(至此已经新建一个按钮了。但怎样加入事件响应)
.
.}
响应函数应该怎样加入。你说的方法我之前已经试过,
button2.Click += new System.EventHandler(this.button2_Click);但是是没有this.button2_Click这个方法的。因为根本还没有定义。应定义在那里。
private void button2_Click(object sender, System.EventArgs e)
{
...}
private void btnTest1_Click(object sender, System.EventArgs e)
{
Button btnTest2 = new Button();
btnTest2.Name = "btnTest2";
btnTest2.Text = "按钮2";
btnTest2.Size = btnTest1.Size;
btnTest2.Location = new Point(btnTest1.Location.X + btnTest1.Size.Width + 5, btnTest1.Location.Y);
btnTest2.Click+=new EventHandler(btnTest2_Click);
this.Controls.Add(btnTest2);
}
private void btnTest2_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello,World");
}
button2.Click += new System.EventHandler(this.button2_Click);但是是没有this.button2_Click这个方法的。因为根本还没有定义。应定义在那里。
========》
再类里面定义就行了,其实手动添加事件设计器会提示你按tab键生成方法的。