Pr.wa'el

Hi . I have two functions handle the same Leave Event of TextBox . Once the event fired , first function will handle it then the second one ( Combined delegates ).

In First function i check for some condition , if this condition is true continue normally .

But when Condition is false , the second function must not be executed .

My Problem is : -

How i can prevent the second function from being execute ( End Event handling )

Can i remove Leave Event's message from program's MessageLoop , or remove it from MessageQueue And how

I need solution as e.Handled=true or e.Cancel which exist in other Event Argument types

Thank you




Re: Visual C# Language stop event handling

RMS81

You could do it by returning a value (true or false) from the first funtion. Something like continue = true or false. Then start the second one with:

if continue=true then

function2

else

exit sub





Re: Visual C# Language stop event handling

decyclone

Hi Pr.wa'el,

Once the execution of the registered functions in the delegate is started, normally, it can not be stopped. And I don't recommend removing messages from windows message loop. One thing you can do is use a flag variable which is accessable to all the functions registered in the event delegate. Each function must check that flag before proceeding.

If you want to avoid this case, try the following example code :

Code Snippet

class MyClass

{

// Custom delegate that returns boolean value indicating success or failure

public delegate Boolean BooleanResultEventHandler(object sender, EventArgs e);

// A dummy/empty object to reference MyEvent eventhandlers in the _handlers Dictionary/HashTable

private static readonly Object _myEvent = new Object();

// A Dictionary/HashTable to store eventhandlers for MyEvent event

private EventHandlerList _handlers = new EventHandlerList();

// An event with custom add and remove accessors

public event BooleanResultEventHandler MyEvent

{

add { _handlers.AddHandler(_myEvent, value); }

remove { _handlers.RemoveHandler(_myEvent, value); }

}

// Custom code to raise the event

private void OnMyEvent()

{

// Retrieve all the handlers as one delegate

BooleanResultEventHandler myEvent = _handlers[_myEvent] as BooleanResultEventHandler;

if (myEvent != null)

{

// Iterate over the handlers and if any one of them returns false, break the loop

foreach (BooleanResultEventHandler d in myEvent.GetInvocationList())

{

if (!d(this, EventArgs.Empty)) break;

}

}

}

}

Reference : here.