我不知道如何打破事件处理程序方法列表。
例如我遵循代码。我应该在IF声明中写什么?
public event EventHandler myEvent;
...
myEvent += new EventHandler(met1);
myEvent += new EventHandler(met2);
myEvent += new EventHandler(met3);
...
public void met2(object sender, EventArgs e)
{
...
if(myCondition)
{
//there I want to break execution of all methods assiciated with myEvent event
//I want to break met2 and don't allow to execute met3
}
...
}
答案 0 :(得分:4)
您可以定义委托,因此您的custom event handler
及其自定义EventArgs
具有布尔值。
示例:
public class MyEventArg : EventArgs {
public bool Handle {get;set;}
}
myEvent += new MyEventHandler(met1);
public void met2(object sender, MyEventArgs e)
{
if(e.Handled)
return;
if(myCondition)
{
e.Handled = true;
return;
}
...
}
这样,如果我们在任何其他事件处理程序之前处理它,检查是否Handled == true
,一次从其中一个设置到该状态,其他人将跳过该事件处理
只是一个想法示例,您必须更改它以满足您的代码的确切需求。
答案 1 :(得分:0)
查看KeyDownEventArgs,可以将属性Handled
设置为true。
你可以做类似的事情:
class myClass {
public event EventHandler myEvent;
myEvent += new EventHandler(met1);
myEvent += new EventHandler(met2);
myEvent += new EventHandler(met3);
public void metN(object sender, MyCustomEventArgs e)
{
if(e.Cancel)
return;
// Do whatever you like
if(<someBooleanStatement>)
{
e.Cancel = true;
return;
}
// Do whatever you like
}
}