C# 动态按钮事件不工作
我正在开发一个winforms程序,它要求我动态地向程序中添加按钮。我已经创建了许多按钮和其他控件,但是这个特定按钮的事件不起作用 这就是我所做的 创建控件类C# 动态按钮事件不工作,c#,winforms,C#,Winforms,我正在开发一个winforms程序,它要求我动态地向程序中添加按钮。我已经创建了许多按钮和其他控件,但是这个特定按钮的事件不起作用 这就是我所做的 创建控件类 public CreateControl(Form form) { this.form = form; } public Button button1; public void AddButton() { button1 = new Button(); this.button1.Location = new S
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();
}
表单类
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();
}
我在按钮1上有一个断点,但它从未触发,而我的其他控件(包括按钮)能够触发各自的事件。有什么我可能遗漏的吗?如果在添加事件处理程序的行上放置一个断点,该行是否执行?啊,谢谢大家,我无意中调用了cc.AddButton();在另一个方法中,首次如此LoadSelectPatientControl()从未被调用。请为这个错误感到愚蠢。