Results 1 to 7 of 7

Thread: beginner question: help with making a same event for a list of pictureboxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    10

    beginner question: help with making a same event for a list of pictureboxes

    hello all
    i have created and set values for a list of 10 pictureboxes like this:

    Dim pictureboxes As List(Of PictureBox) = New List(Of PictureBox)

    For i = 1 To 10

    Dim P As PictureBox = New PictureBox
    P.Size = New Size(120, 180)
    ...
    pictureboxes.Add(P)
    Me.Controls.Add(P)

    Next i

    i now want to create some sub to deal events with these pictureboxes (same event for all pictureboxes, drag and drop in my program);

    i know i can make it with 10 sub like this

    AddHandler pictureboxesr(0).Mousedown, AddressOf PictureBoxes_Mousedown
    ...
    Private Sub PictureBoxes_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBoxes(0).MouseDown

    same with pictureboxes(1) and so on

    i wondered if there was a better and simpler way to do it ; i mean calling the same sub for all pictureboxes, with transmission of an argument determining the number of imagebox concerned




    thanks for your answers

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

    Re: beginner question: help with making a same event for a list of pictureboxes

    The first question is, if you know exactly how many PictureBoxes you need, why are you creating them in code rather than in the designer? Are you perhaps going to add more later?

    If you create an indeterminate number of objects in code then the only way to attach an event handler is to use AddHandler. If you do that then the method shouldn't have a Handles clause at all and you must also make sure that you use RemoveHandler to detach. If you add the controls in the designer then you can use the IDE to generate the event handler for you with all the events listed in the Handles clause.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    10

    Re: beginner question: help with making a same event for a list of pictureboxes

    well indeed the problem is that i try to make a kind of cards game and i would have liked to use the same "drag and drop" sub to move my cards; but i can have an indeterminate number of them
    if i understand , i will have to generate as many subs as pictureboxes

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: beginner question: help with making a same event for a list of pictureboxes

    Quote Originally Posted by kirileclown View Post
    well indeed the problem is that i try to make a kind of cards game and i would have liked to use the same "drag and drop" sub to move my cards; but i can have an indeterminate number of them
    if i understand , i will have to generate as many subs as pictureboxes
    You only need one method regardless. I'm not saying any different. I was questioning your use of AddHandler because your code was specifically creating 10 controls which would be better done in the designer. If that's not your actual code and you are creating a number controls that won't be know until run time then using AddHandler is appropriate, but you'll need to use RemoveHandler too.

    If you create the controls in the designer then just add them all to the one Handles clause, e.g.
    Code:
    Private Sub PictureBoxes_MouseDown(sender As Object,
                                       e As MouseEventArgs) Handles PictureBox1.MouseDown,
                                                                    PictureBox2.MouseDown,
                                                                    PictureBox3.MouseDown
    The IDE can create that method with the Handles clause for you. If you're using AddHandler then get rid of the Handles clause altogether. Either way, inside the method, the `sender` parameter provides a reference to the object that raised the event.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    10

    Re: beginner question: help with making a same event for a list of pictureboxes

    OK its clear thanks for your answer jm

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: beginner question: help with making a same event for a list of pictureboxes

    One thing to note about card games is that you almost certainly have a fixed number of locations where the cards can be displayed. The number of locations depends on the game, but within any one game the total number of locations is fixed. Only those locations need to have controls, so you can always build them at design time. The card shown at that location is a different matter (the .Tag property of the control may be useful for holding the card), but that is separate from the control itself. This is convenient, because you can generate a single set of methods to handle the drag and drop functionality (for drag and drop you will always have three handlers: MouseDown, DragEnter, DragDrop, and you will occasionally have a few others, such as ProvideFeedback) for all the controls at once from the designer. The designer can build a method with all the Handles clauses already in place... though I generally forget this and end up doing it by hand on the rare occasions when I write such a thing.
    My usual boring signature: Nothing

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: beginner question: help with making a same event for a list of pictureboxes

    From everything I've read, if you're adding handlers to objects that are created and added to the form's control collection, and they exist for the duration of the life of the form, you don't need to worry about using RemoveHandler. You need to use RemoveHandler if you are going to remove objects while any subscribers that may raise events on those handlers continue to exist.
    If you create and add them to the form, all event handlers added to the form will be cleaned up when the form is destroyed.

    Just for one example of a partially completed card game, in post #6 of this thread is a card game implemented using pictureboxes dynamically created for several of the elements, including the graphical representation of the cards. In post #12, is the same code reimplemented to not use pictureboxes for the graphical elements, it draws the card images instead.

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