Bob Kaufman
Bob Kaufman

Reputation: 12815

ArgumentException constructing RoutedEventArgs with MouseDownEvent

I am trying to simulate a mouse click on a RichEditControl. The underlying problem I am trying to solve is to set Focus on this control if the user takes another action without ever having clicked the RichEditControl first. Seems that if you bring up a form and never click the RichEditControl, something isn't initialized and the cursor won't appear.

I have read several helpful blogs and SO posts on the matter, and they all seem to point me to doing something of the form

RichEdit.RaiseEvent( new RoutedEventArgs( <something> );

I've written these four lines of code:

1. RichEdit.RaiseEvent( new RoutedEventArgs(Button.ClickEvent));
2. RichEdit.RaiseEvent( new RoutedEventArgs(RichEditControl.MouseDoubleClickEvent));
3. RichEdit.RaiseEvent( new RoutedEventArgs(RichEditControl.MouseDownEvent));
4. RichEdit.RaiseEvent( new RoutedEventArgs(RichEditControl.MouseUpEvent));

(1) does nothing. This makes sense because ClickEvent belongs to Button, not RichEditControl. (2) does nothing. It's a double-click. I want a click.

(3) and (4) are what's tripping me up. I'm getting an ArgumentException

Object of type 'System.Windows.RoutedEventArgs' cannot be converted to type 'System.Windows.Input.MouseButtonEventArgs'.

This makes no sense to me. MouseDownEvent is of type RoutedEvent. the constructor to RoutedEventArgs is expecting a RoutedEvent as an argument. RaiseEvent() is expecting an argument of type RoutedEventArgs. Everything compiles fine without warnings. Looking at the stack trace, the exception occurs deeper, at System.RuntimeType.TryChangeType.

While I have serious doubts this will solve my underlying problem, I still want to try to force-feed a mouse click to the RichEditControl to see if this solves my problem. So my question is, why is MouseDoubleClickEvent causing an ArgumentException when, from what I can tell, I'm passing in correct types?

Upvotes: 0

Views: 245

Answers (0)

Related Questions