Results 1 to 4 of 4

Thread: [RESOLVED] Addhandler for custon event with MouseEventArgs

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [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?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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).
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width