【发布时间】:2019-01-15 16:06:50
【问题描述】:
我们遇到的问题是访问在另一个按钮的单击事件中创建的按钮的单击事件,即单击第一个按钮会生成一个新面板和控件,我们现在希望这个新创建的面板上的按钮执行一个动作。
控件已在类的顶部声明如下:
Panel createElementPage = null;
TextBox elementDescription = null;
TextBox elementName = null;
Button continueButton = null;
AuditSystem audit;
这里摘录生成新面板的方法,定义 continueButton 的部分写法如下:
public void CE_Click(object sender, EventArgs e)
{
createElementPage.Controls.Add(elementDescription);
continueButton = new Button();
continueButton.Text = "Continue";
continueButton.Location = new Point(700, 500);
continueButton.Size = new Size(100, 50);
createElementPage.Controls.Add(continueButton);
}
我们想要访问 continueButton 的 click 事件处理程序,但我们编写的方法似乎不起作用。这是我们目前所拥有的:
private void continueButton_Click(object sender, EventArgs e)
{
Console.WriteLine(" something");
}
单击按钮不会产生任何结果,我们尝试了一些解决方案,例如实现单独的 eventHandler 方法。有人可以解决这个问题吗?
【问题讨论】:
-
需要订阅点击事件
continueButton.Click += continueButton_Click;
标签: c# .net winforms onclick event-handling