[RESOLVED] Addhandler for custon event with MouseEventArgs
I managed to to raise an event from a UserControl which is then handled in the MainForm (See this Thread)
I've done
In UserControl:
Declaring Public Event by
Code:
Public Event PublicEventUserGram As EventHandler
Raising the Event in correct Event-Routine with the correct MyMouseEventArg by
Code:
RaiseEvent PublicEventUserGram(Me, MyMouseEventArg)
In the parent form:
Changing the PictureBox_MouseUp to be Private
When creating new instances of the UserControl adding the EventHandle by
Code:
AddHandler NewUserGram.PublicEventUserGram, AddressOf PictureBox_MouseUp
However, I had Option Strict OFF in the MainForm. After selection it to ON, I get an error saying an implicit typeconversion from MouseEventArgs to EventArgs was needed.
If I declare the Public Event explicitly with "(ByVal sender As Object, ByVal e As MouseEventArgs)" I get an error that an event can't have a return value.
Do I need to create a Sub with perfect matching arguments in the mainform, that in turn changes to EventArg to a MouseEventArg an then calls the needed Sub?
Or is there a better way?
Re: Addhandler for custon event with MouseEventArgs
Um, if your event declaration is like this:
Code:
Public Event PublicEventUserGram As EventHandler
then you don't raise the event like this:
Code:
RaiseEvent PublicEventUserGram(Me, MyMouseEventArg)
You raise it like this:
Code:
RaiseEvent PublicEventUserGram(Me, EventArgs.Empty)
If you do want to raise it like this:
Code:
RaiseEvent PublicEventUserGram(Me, MyMouseEventArg)
then you declare it like this:
Code:
Public Event PublicEventUserGram As EventHandler(Of MyMouseEventArg)
The event handler should then look like this:
Code:
Private Sub SomeMethod(sender As Object, e As MyMouseEventArg)
You can handle the event with a method like this:
Code:
Private Sub SomeMethod(sender As Object, e As EventArgs)
because MyMouseEventArg inherits EventArgs but, unless MyMouseEventArg inherits MouseEventArgs, you cannot handle it with a method that looks like this:
Code:
Private Sub SomeMethod(sender As Object, e As MouseEventArgs)
By the way, you should follow convention and name your class MyMouseEventArgs.
Re: Addhandler for custon event with MouseEventArgs
You might benefit from following the Blog link in my signature below and checking out my post on Custom Events.
Re: Addhandler for custon event with MouseEventArgs
Thanks jmcilhinney,
using the correct declaration was what I needed.
Sorry for the typo in MyMouseEventArgs, I did not try to come up with a new naming concept, just missed the s (and copied that mistake throughout).