Results 1 to 6 of 6

Thread: [RESOLVED] AddHandler from a module, is that the correct way?

  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 from a module, is that the correct way?

    I'm trying to use only the suggested ways to interfere with the controls on my forms.
    Here is a specific question:
    I need to AddHandles during Runtime. Actually I do that from a module like:
    vb Code:
    1. For Each g As usrGram In MainForm.GramList
    2.    AddHandler b.PingerPing, AddressOf g.Ping_zeichnen 'b as an ObjectType with a Event called PingerPing
    3. Next

    Is that the recommended way or should I try to move that code into the class of the Form (using an Event to trigger it)?
    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 dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: AddHandler from a module, is that the correct way?

    Personally the way that I would do it is the latter. The reason is because what if you decide to change your project around and you no longer have a MainForm or MainForm is a completely different form? That can be avoided by simply calling Me.<some sort of collection>.


    Edit - To elaborate a bit more; if GramList is a custom class, then it may be better to create the handlers in that class as opposed to on the main form. But you'd have to weigh out if it's more beneficial to do that, which really we can't judge without seeing the class itself(assuming it even is a class, it could just be a list of <t>).
    Last edited by dday9; Dec 10th, 2013 at 03:28 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: AddHandler from a module, is that the correct way?

    this is the correct way:

    Code:
    Public Class UserControl1
    
        Public Event myEvent(ByVal sender As Object, ByVal e As MouseEventArgs)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            RaiseEvent myEvent(Me, New MouseEventArgs(Windows.Forms.MouseButtons.Left, 1, 0, 0, 0))
        End Sub
    
    End Class
    Name:  10-12-2013 20.49.32.jpg
Views: 295
Size:  49.0 KB

  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 from a module, is that the correct way?

    @ dday9 My Gramlist is a Collection of UserControls.
    @.paul. The problem with the solution to add the handler in UserControl Class is that it needs the add and remove for each creation and deletion of the other Object-Type ( b in the example).
    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!

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: AddHandler from a module, is that the correct way?

    if it's an event of an object in a uc, + the handler is in the uc, you should setup the handling in the uc
    if it's an event of an object not in a uc, + the handler is in the uc, your code is ok

  6. #6

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

    Re: AddHandler from a module, is that the correct way?

    Quote Originally Posted by .paul. View Post
    if it's an event of an object not in a uc, + the handler is in the uc, your code is ok
    It's this situation, sorry for not telling in the first place.
    Thanks for the advice, Solved!
    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