0

I use the notifyIcon from Winforms in a WPF application. Bellow is part of my event handler:

private void notifyIcon_Logger_MouseDown( object sender, EventArgs e )
{
        var st = e.ToString();
...

I may not make e parameter a MouseEventArgs because compiler says it does not match. But even so, I see that st is "System.Windows.Forms.MouseEventArgs". How is that?!

I have pinned e on IDE surface to watch it for debug purposes and I see it has a member Button. I see something like

Button = Right

but if I try e.Button I get error CS1061: 'EventArgs' does not contain a definition for 'Button' How is all these possible? More importantly, how to identify mouse button?

Gigi
  • 158
  • 11
  • You have to cast EventArgs to the proper extended type. Thats why you get this error since EventArgs is just the base type and indeed has no property named Button. To make the parameter type match just fully qualify the forms type since wpf also has a MouseEventArgs type defined. – BionicCode Apr 28 '18 at 06:40
  • @BionicCode And how to do that? If I simply try ((MouseEventArgs)e).Button I get an error, of course. – Gigi Apr 28 '18 at 09:03
  • I don't know where you're headed with this but maybe this would be useful: https://www.codeproject.com/Articles/36468/WPF-NotifyIcon – Andy Apr 28 '18 at 09:05
  • what error? Cast to "System.Windows.Forms.MouseEventArgs" – BionicCode Apr 29 '18 at 07:15
  • You are just casting to System.Windows.Input.MouseEventArgs right now and this type doesn't have a property named Button. You are in the wrong namespace. – BionicCode Apr 29 '18 at 11:51

1 Answers1

0

mixing WPF and Winforms can sometimes be tricky...

There are 2 types called MouseEventArgs. One is the WPF version in the System.Windows.Input namespace and the other is the Winforms version in the System.Windows.Forms namespace.

By simply casting it as MouseEventArgs the compiler uses the WPF form since this is WPF app but you need the Winforms version since this particular callback is for a Winforms control. So simply qualify it with the correct namespace in the callback definition...

private void notifyIcon_Logger_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e )
{
        var st = e.ToString();
...
Jeff R.
  • 1,493
  • 1
  • 10
  • 14