无法以编程方式点击添加按钮
问题描述:
我正在以编程方式向表单添加按钮,出于某种原因,我无法单击它。为什么不?无法以编程方式点击添加按钮
private Button btnBrowser = new Button();
this.btnBrowser.Text = "Open Browser";
this.btnBrowser.Location = new System.Drawing.Point(55, 45);
this.btnBrowser.Size = new System.Drawing.Size(70, 30);
这会将按钮添加到窗体,但我无法单击它。
private void btnBrowser_Click(object sender, EventArgs e)
{
MessageBox.Show("test");
}
答
确保你将它添加到窗体,并添加事件处理程序:
this.Controls.Add(btnBrowser);
btnBrowser.Click += btnBrowser_Click;
答
var btnBrowser = new Button();
btnBrowser.Text = "Open Browser";
btnBrowser.Location = new System.Drawing.Point(55, 45);
btnBrowser.Size = new System.Drawing.Size(70, 30);
btnBrowser.Click += (o, evt) =>
{
MessageBox.Show("test");
};