Pagates:
This just breaks into a few steps:
1. Define a custom event args class that contains your flag
2. Define a delegate that accepts your evetargs as a parameter.
3. A little extra work in your event invocation.
(you have some decisions to make with #3)
// warning: the code is off the cuff, might contain syntax errors.
public class MyEventArgs: System.EventArgs
{
public bool EventHandled; // here's your flag
}
public delegate void MyEventHandler(object sender, MyEventArgs e);
public class MyClass
{
public event MyEventHandler MyEvent;
protected bool RaiseMyEvent()
{
MyEventHandler myEvent = this.MyEvent;
if(handler == null)
return false;
MyEventArgs e = new MyEventArgs();
// remember you might have multiple event subscribers
// check your args for each subscriber
bool eventHandled = false;
foreach( MyEventHandler handler in myEvent.GetInvocationList())
{
handler(this, e);
if(e.EventHandled)
eventHandled = true;
}
return eventHandled;
}
}
Another way to do it would be to exit the foreach loop as soon as someone
handles the event. If you exit the foreach loop as soon as someone sets the
handled flag, it means that not all subscribers will receive the event.
If it is more appropriate that all subscribers receive the event, just set a
local bool so that you can remember that one of the subscribers set the flag
"pagates" <pa*****@discussions.microsoft.com> wrote in message
news:C1**********************************@microsof t.com...
Hello All,
Does anybody have an example to raise an event that may or may not give
feedback to the caller? I am thinking about something roughly like the
Cancel property of an CancelEventArgs or the Handled property on the
KeyDown
events.
What I have is a user control that I want to raise an event that allows
the
parent to set a property in the control, and set a flag in the event that
says it has set the property. The user control would then process
conditional code if the parent responds to the event (or does not).
Thanks,
pagates