handling mousedown for dynamic pictureboxes

S

Scott Mackay

Hi,

I'm using visual studio dotnet 2002 programming in vb

can anyone tell me how I can handle the mousedown event for pictureboxes I
create dynamically at runtime?
I've tried setting the mousedown to handle mybase.mousedown but it doesn't
detect the mousedown event until I move out of the pictureboxes I have
created (only works on the part of the form where I don't have any
pictureboxes). I also cannot set the mousedown event to handle my array of
pictureboxes as they are not a valid handle.

regards,

Scott M.
 
H

Herfried K. Wagner [MVP]

Hello,

Scott Mackay said:
can anyone tell me how I can handle the mousedown
event for pictureboxes I create dynamically at runtime?

Have a look at the AddHandler and RemoveHandler keywords.

HTH,
Herfried K. Wagner
 
T

Tom Spink

Hi, you can use AddHandler to add an event handler for an, erm, event :)

'///
AddHandler MyRuntimePictureBox.MouseDown, AddressOf MyMouseDownHandler
'///


Public Sub MyMouseDownHandler(ByVal sender As Object, ByVal e As
MouseEventArgs)
 
S

Scott Mackay

Hi again,

okay follow up question:

now that I have handled the clicking of one of my array of pictureboxes, how
can I tell which picturebox within the array I have clicked on?

regards,

Scott M.
 
F

Fergus Cooney

Hi Scott,

The sender argument of the event handler will be the PictureBox that was
clicked. It's defined as an Object, so cast it to a PictureBox and then loop
through your array until you find a match.

Regards,
Fergus.

=============================
Public Shared Moan
Sender - now what kind of name is that ?
I've never read Sender and thought "ah,
that must be the control that the event
occurred with/in/at/to."
End Moan
 
T

Tom Spink

Hi, your attached method has a sender argument, which can be casted to the
PictureBox which was clicked:

'///
Dim pbClicked As PictureBox
If TypeOf sender Is PictureBox Then pbClicked = DirectCast(sender,
PictureBox)
'///

--
Happy to help,
-- Tom Spink
([email protected])

"Go down with your server"

http://dotnetx.betasafe.com >> On The Mend

Please respond to the newsgroup,
so all can benefit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top