Question Adding an event to a picturebox

NKN

Member
Joined
Dec 9, 2011
Messages
19
Programming Experience
Beginner

For Each pb As PictureBox In Me.Controls.OfType(Of PictureBox)().Except(New PictureBox() {PictureBox1, PictureBox2}) If PictureBox1.Bounds.IntersectsWith(pb.Bounds) Then MsgBox("ok") End If
How can i add an event to the picturebox that intersects with picturebox1 without adding it to all the other pictureboxes?
 
Do you really mean add an event, or do you actually mean handle an event, i.e. attach an event handler? If it's the latter then you would presumably use the AddHandler statement inside the If statement and specify the current PictureBox, i.e. 'pb', as the target. Make sure you use RemoveHandler when you no longer want the event handled though.
 
If what you say is what you really mean then it's not possible. If you want to add an event to a class then you have to do so when you define the class.

If what you really mean is that you want to handle an event then I've already told you what to do. Maybe, instead of saying "an event" you could explain exactly what this event is and what is supposed to happen when it's raised. If you provide us with a FULL and CLEAR explanation of the problem then we can probably provide good advice on how to solve it. If you provide as brief a description as possible and expect us to guess or make assumptions then you'll be lucky to get the information you want.
 
I have a picturebox1 that i can move with WASD and another 40 pictureboxes(pb).
what i want is when the picturebox1 intersects with one of the 40 picturesboxes that this same picturebox dissapear (dispose).
how can i do that?
 
It sounds to me the event you're looking for is the KeyDown event, where in event handler you move the picturebox by WADS, perform hittesting and act accordingly.
 
I've done the part for moving the picturebox with WASD.
But what i want is when the picturebox1 intersects with one of the 40 picturesboxes ("pb" in the code) that this same picturebox dissapear/dispose (and not all the pictureboxes "pb").
how can i do that?




VB.NET:
For Each pb As PictureBox In Me.Controls.OfType(Of PictureBox)().Except(New PictureBox() {PictureBox1, PictureBox2})
            If PictureBox1.Bounds.IntersectsWith(pb.Bounds) Then
                MsgBox("it worked")
 
So this actually has nothing whatsoever to do with events and the question is actually how to remove a PictureBox from a form. The answer is the you call Remove on the form's Controls collection and specify the control to remove, ie. the PictureBox. You would then call Dispose on the PictureBox itself.

The one other change you may need is to call ToArray on the list you're creating on the For Each line.
 
Back
Top