如何只添加一次eventHandler? [英] How to Add an eventHandler only one time?
问题描述
嗨!
" CDemo.Call" eventHandler被添加到button3.click
单击button1和button2时的事件。
但是,我想添加cd.Call只有一次即使
我点击了button1和button2。
例如,
1.点击button1
2.点击button2
3.点击button3
4.显示一个MessageBox
怎么办这个工作?
私有System.Windows.Forms.Button button1;
私有System.Windows.Forms.Button button2;
私有系统。 Windows.Forms.Button button3;
public CDemo cd = new CDemo();
..
private void button1_Click(对象发送者,System.EventArgs
e)
{
button3.Click + = new EventHandler(cd.Call);
}
private void button2_Click(对象发件人,System.EventArgs
e)
{
button3.Click + = new EventHandler(cd.Call);
}
..
..
..
公共类CDemo
{
public void Call(object sender,System.EventArgs e)
{
MessageBox.Show(" Demo");
}
}
Hi!
"CDemo.Call" eventHandler is added to "button3.click"
Event when both button1 and button2 are clicked.
However, I want to add "cd.Call" only one time even though
I clicked both button1 and button2.
For Example,
1. Click button1
2. Click button2
3. Click button3
4. One MessageBox is shown
How to do this job?
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
public CDemo cd = new CDemo();
..
private void button1_Click(object sender, System.EventArgs
e)
{
button3.Click += new EventHandler(cd.Call);
}
private void button2_Click(object sender, System.EventArgs
e)
{
button3.Click += new EventHandler(cd.Call);
}
..
..
..
public class CDemo
{
public void Call(object sender, System.EventArgs e)
{
MessageBox.Show("Demo");
}
}
推荐答案
private bool handlerAdded = false;
private void button1_Click(object sender,System.EventArgs e)
{
if(!handlerAdded){
button3.Click + = new EventHandler(cd.Call);
handlerAdded = true;
}
}
和button2_Click类似。
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请回复到新闻组。
private bool handlerAdded = false;
private void button1_Click(object sender, System.EventArgs e)
{
if ( !handlerAdded ) {
button3.Click += new EventHandler(cd.Call);
handlerAdded = true;
}
}
and similarly for button2_Click.
Mattias
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
嗨!
即使你的代码提供了一个解决方案,我也无法使用你的
代码。
我不想使用另一个全局变量来解决
这种情况。
我必须同时解决这个问题对象
实例button3和cd。
谢谢,
Jongmin
Hi!
Even your code provides one solution, I can''t use your
code.
I don''t want use another global variable in order to solve
this situation.
I have to solve this problem only with both object
instance button3 and cd.
Thanks,
Jongmin
-----原始消息-----
私有bool handlerAdded = false;
void void button1_Click(对象发送者,
系统。 EventArgs e){
if(!handlerAdded){
button3.Click + = new EventHandler(cd.Call);
handlerAdded = true;
}
} <对于button2_Click也是如此。
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ |
http://www.dotnetinterop.com 请仅回复新闻组。
。
-----Original Message-----
private bool handlerAdded = false;
private void button1_Click(object sender, System.EventArgs e){
if ( !handlerAdded ) {
button3.Click += new EventHandler(cd.Call);
handlerAdded = true;
}
}
and similarly for button2_Click.
Mattias
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.comPlease reply only to the newsgroup.
.
怎么样:
private void button1_Click(...){
button3.Click - = new EventHandler(cd.Call);
button3.Click + = new EventHandler(cd.Call);
}
private void button2_Click(...){
button3.Click - = new EventHandler(cd.Call);
button3.Click + = new EventHandler(cd.Call);
}
只保留一个事件处理程序。
how about this:
private void button1_Click(...) {
button3.Click -= new EventHandler(cd.Call);
button3.Click += new EventHandler(cd.Call);
}
private void button2_Click(...) {
button3.Click -= new EventHandler(cd.Call);
button3.Click += new EventHandler(cd.Call);
}
Keeps only one event handler.
这篇关于如何只添加一次eventHandler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!