【发布时间】:2013-04-16 16:08:45
【问题描述】:
我想动态添加一个从form1到form2的点击事件。
这是我在 form1 中的代码:
Form2 frm = new Form2();
string title =(string)listBox1.SelectedItem;
TabPage myTabPage = new TabPage(title);
frm.tabControl1.TabPages.Add(myTabPage);
//create button and it's event
Button button1 = new Button();
button1.Click += new System.EventHandler(button1_Click);
button1.Location = new Point((myTabPage.Width/2)-(button1.Width/2),myTabPage.Height-30);
button1.Text = "Click On Me!";
myTabPage.Controls.Add(button1);
frm.Show();
我收到以下错误: 当前上下文中不存在名称“button1_Click”
请帮忙。
【问题讨论】:
-
您是否定义了 button1_Click 新表单的任何位置?如果不是,则必须在将其绑定到任何按钮单击事件之前对其进行定义。