passing data with an eventhandler

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

At runtime I generate 8 pictureboxes, when the user click on a picturebox I
fire an onclick eventhandler. Because these pictureboxes are generate at
runtime I do not have direct access to the their name property.

I need to know the name of the picturebox that the user click on so my
question is,
is it possible to pass data with en evenhandler like onclick? or is there a
another approach to
this that I am missing?
 
OfurG?rn said:
At runtime I generate 8 pictureboxes, when the user click on a picturebox I
fire an onclick eventhandler. Because these pictureboxes are generate at
runtime I do not have direct access to the their name property.

I need to know the name of the picturebox that the user click on so my
question is,
is it possible to pass data with en evenhandler like onclick? or is there a
another approach to
this that I am missing?

Why not pass the picturebox as the "source" argument of the event
handler?
 
Hi,



OfurGørn said:
At runtime I generate 8 pictureboxes, when the user click on a picturebox
I
fire an onclick eventhandler.

Correctely speaking you handle the Click event that the picturebox fires
when he receive a click, you do not fire nothing, you receive it.
Because these pictureboxes are generate at
runtime I do not have direct access to the their name property.

You will have the same situation if the same handler (or the same method)
handles more than one event or the same event fired by more than one
control.
I need to know the name of the picturebox that the user click on so my
question is,
is it possible to pass data with en evenhandler like onclick? or is there
a
another approach to
this that I am missing?

The first parameter of EventHandler gives you who sent it, IF you know for
sure that only a PictureBox instance will fire it you can cast the sender
parameter to PictureBox.
If you are not sure you can cast it to a Control
 
As a follow up to Jon and Ignacio's posts, you can set a name property on a
dynamically generated control when it is created. When the event is fired,
you can get this property out of the

((PictureBox)sender).Name;

in your eventhandler code.

--Peter
 
"OfurGørn" <[email protected]> a écrit dans le message de (e-mail address removed)...

| At runtime I generate 8 pictureboxes, when the user click on a picturebox
I
| fire an onclick eventhandler. Because these pictureboxes are generate at
| runtime I do not have direct access to the their name property.
|
| I need to know the name of the picturebox that the user click on so my
| question is,
| is it possible to pass data with en evenhandler like onclick? or is there
a
| another approach to
| this that I am missing?

The sender parameter to the event handler *is* the component that triggers
the event.

Simply cast the sender to Control and then you can access the Name property.

Joanna
 
OfurG?rn said:
I was afraid it was something simple.

Never be afraid that the solution will be simple. Be hopeful that it'll
be simple, and afraid that it will be complicated :)
 
Hi ,

I just wanted to check how things are going. If there is any question,
please feel free to join the community and we are here to support you at
your convenience. Thanks again and Happy New Year!

Best Regards,

Terry Fei [MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Back
Top