我正在开发一个winforms程序,它要求我动态地向程序添加按钮。我已经创建了许多按钮和其他控件,但是这个特定按钮的事件不起作用。
这就是我所做的。
创建控件类
public CreateControl(Form form) {
this.form = form;
}
public Button button1;
public void AddButton() {
button1 = new Button();
this.button1.Location = new System.Drawing.Point(387, 665);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "View";
this.button1.UseVisualStyleBackColor = true;
form.Controls.Add(this.button1);
}
表单类
// cc is defined in the constructor as an object of CreateControl
private void loadSelectPatientControls() {
cc.AddButton();
cc.button1.Click += new System.EventHandler(this.button1_Click);
}
private void button1_Click(object sender, EventArgs e) {
ShowList();
}
我在button1_Click
上有一个断点,但它从未被解雇,而我的其他控件(包括按钮)能够触发各自的事件。有什么我可能错过的吗?