-
Aug 31st, 2014, 04:20 AM
#1
Thread Starter
New Member
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
-
Aug 31st, 2014, 04:32 AM
#2
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.
-
Aug 31st, 2014, 06:21 AM
#3
Thread Starter
New Member
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
-
Aug 31st, 2014, 07:22 AM
#4
Re: beginner question: help with making a same event for a list of pictureboxes
Originally Posted by kirileclown
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.
-
Aug 31st, 2014, 09:35 AM
#5
Thread Starter
New Member
Re: beginner question: help with making a same event for a list of pictureboxes
OK its clear thanks for your answer jm
-
Aug 31st, 2014, 10:21 AM
#6
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
-
Aug 31st, 2014, 05:23 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|