C# 移除处理程序到按钮
我试图从按钮中删除一个处理程序(单击),我尝试了很多方法,但都没有成功 我创建了如下两个按钮:C# 移除处理程序到按钮,c#,C#,我试图从按钮中删除一个处理程序(单击),我尝试了很多方法,但都没有成功 我创建了如下两个按钮: private void CreateButtons() { Button button1 = new Button() { Text = "Start" }; Button button2 = new Button() { Text = "Stop" }; this.Controls.Add(button1);
private void CreateButtons()
{
Button button1 = new Button()
{
Text = "Start"
};
Button button2 = new Button()
{
Text = "Stop"
};
this.Controls.Add(button1);
button1.Click += Button1_Click;
this.Controls.Add(button2);
button2.Click += Button2_Click;
}
第一个只是创建了一个MessageBox:
private void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}
第二个应移除处理程序:
private void Button2_Click(object sender, EventArgs e)
{
button1.Click -= Button1_Click;
}
不幸的是,单击按钮2后,按钮1会继续生成MessageBox。我做错了什么?您的表单似乎有一个与您以前创建的按钮不同的按钮,名为
button1
删除它,并将按钮1
和按钮2
字段设置为表单的字段
public partial class Form1 : Form
{
private Button button1;
private Button button2;
public Form1()
{
InitializeComponent();
}
private void CreateButtons()
{
button1 = new Button()
{
Font = new Font(Font.FontFamily, 10, FontStyle.Bold),
BackColor = Color.Yellow,
Width = 79,
Height = 62,
Location = new Point(141, 191),
Text = "Start"
};
button2 = new Button()
{
Font = new Font(Font.FontFamily, 10, FontStyle.Bold),
BackColor = Color.Yellow,
Width = 79,
Height = 62,
Location = new Point(338, 191),
Text = "Stop"
};
this.Controls.Add(button1);
button1.Click += Button1_Click;
this.Controls.Add(button2);
button2.Click += Button2_Click;
}
private void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}
private void Button2_Click(object sender, EventArgs e)
{
button1.Click -= Button1_Click;
}
private void Form1_Load(object sender, EventArgs e)
{
CreateButtons();
}
}
您的表单中似乎有一个以前创建的名为
button1
的不同按钮
删除它,并将按钮1
和按钮2
字段设置为表单的字段
public partial class Form1 : Form
{
private Button button1;
private Button button2;
public Form1()
{
InitializeComponent();
}
private void CreateButtons()
{
button1 = new Button()
{
Font = new Font(Font.FontFamily, 10, FontStyle.Bold),
BackColor = Color.Yellow,
Width = 79,
Height = 62,
Location = new Point(141, 191),
Text = "Start"
};
button2 = new Button()
{
Font = new Font(Font.FontFamily, 10, FontStyle.Bold),
BackColor = Color.Yellow,
Width = 79,
Height = 62,
Location = new Point(338, 191),
Text = "Stop"
};
this.Controls.Add(button1);
button1.Click += Button1_Click;
this.Controls.Add(button2);
button2.Click += Button2_Click;
}
private void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello World");
}
private void Button2_Click(object sender, EventArgs e)
{
button1.Click -= Button1_Click;
}
private void Form1_Load(object sender, EventArgs e)
{
CreateButtons();
}
}
Button2\u Click事件如何获得button1对象的链接?看起来您正在使用CreateButtons方法创建它们,因此,我不确定引用来自何处。我认为您的表单中有一个名为
button1
的单独按钮,它不是您正在创建的按钮。您在CreateButtons中定义了button1,因此必须有两个button1实例。您的Button2\u Click事件如何获得button1对象的链接?看起来您是在CreateButtons方法中创建它们的,所以我不确定引用来自何处。我认为您的表单中有一个名为button1
的单独按钮,它不是您要创建的按钮。您在CreateButtons中定义了button1,因此必须有两个button1实例。这实际上是我的问题!在我的设计器中,有一个按钮1的代码。谢谢你的帮助,这其实是我的问题!在我的设计器中,有一个按钮1的代码。谢谢你的帮助