-2

Option Strict throws some errores in these signatures:

Private Sub NotifyIcon_Systray_Click(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles RadMenuItem_Hide.Click,
        RadMenuItem_Show.Click,
        NotifyIcon_Systray.MouseDoubleClick

End Sub

Private Sub RadListControl_ProcessList_DoubleClick(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles RadListControl_ProcessList.DoubleClick

End Sub

The original signatures are: sender As Object, e as EventArgs but I changed them to MouseEventArgs because I need to determine the button pressed with the peoperty e.Button.

How can fix this problem?

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

2 Answers2

1

You can't arbitrarily change the type of that second parameter. The Click event passes an EventArgs object to its handlers. Your changing the type of the parameter won't magically change the type of that object. You can only use a parameter of type MouseEventArgs if the signature of the event matches that. It does for MouseClick and MouseDoubleClick but it does not for Click or DoubleClick. You can't change that.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • `You can't change that.` I changed it as you seen, and it works perfectlly, I can receive an `e` object of type `MouseEventargs` with the `e.Button` property working properly for both `Click` and `DoubleClick` events. – ElektroStudios Sep 28 '14 at 08:14
  • @ElektroStudios - It works perfectly? I thought you said you were getting Option Strict errors? – Chris Dunaway Sep 29 '14 at 13:54
  • But If I disable Option Strict I can compile and it works... because like people said above MouseEventargs derives from Eventargs, thanks for your help – ElektroStudios Sep 29 '14 at 20:03
1

The behaviour is quite strange but you can do the following:

Option Strict On
Public Class Form1
    Private Event NewClick(sender As Object, e As MouseEventArgs)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RaiseEvent NewClick(sender, CType(e, MouseEventArgs))
    End Sub

    Private Sub NewClickHandler(sender As Object, e As MouseEventArgs) Handles Me.NewClick
        MsgBox(e.X & "x" & e.Y & vbCrLf & e.Button.ToString)
    End Sub
End Class

You basically handle the correct signature, cast e to MouseEventArgs and then reraise a custom event that has a signature with MouseEventArgs.

Edit: I have dug around a bit in the System.Windows.Forms.Control class and the reason that this works is, that the control's OnClick event is actually raised with a MouseEventArgs and not a simple EventArgs. It's just then cast to EventArgs (which obviously works since MouseEventArgs inherits from EventArgs).

From the Control code:

If flag AndAlso Not AddressOf Me.ValidationCancelled Then
    If Not Me.GetState(67108864) Then
        Me.OnClick(New MouseEventArgs(button, clicks, NativeMethods.Util.SignedLOWORD(AddressOf m.LParam), NativeMethods.Util.SignedHIWORD(AddressOf m.LParam), 0))
        Me.OnMouseClick(New MouseEventArgs(button, clicks, NativeMethods.Util.SignedLOWORD(AddressOf m.LParam), NativeMethods.Util.SignedHIWORD(AddressOf m.LParam), 0))
    Else
        '...
    End If
End If
Jens
  • 6,275
  • 2
  • 25
  • 51