Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is an exception being raised when I mark a RoutedEventArgs event as Handled?

I am trying to mark an event as handled but an exception gets thrown when I try doing so.

I have the following function that gets called on the LostFocus event of a TextBox:

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
   // ...
   // do something
   // ...

   e.Handled = true;
}

However, the following exception gets raised as soon as e.Handled = true is executed:

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationCore.dll

Additional information: Every RoutedEventArgs must have a non-null RoutedEvent associated with it.

I have tried moving the e.Handled line before the other code in the function but that doesn't help.

Why is the exception being raised and what are some possible workarounds?

like image 287
Aamir Avatar asked Oct 21 '25 03:10

Aamir


1 Answers

Figured it out. TextBox_LostFocus was being called from elsewhere in the application with the event parameter being null.

Hence, for some function calls to TextBox_LostFocus, e.Handled didn't work (since e was null) but worked in some other scenarios (when it was actually called by the framework).

like image 122
Aamir Avatar answered Oct 23 '25 17:10

Aamir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!