control questions

A

Analizer1

Hi hows it going

ive made dynamic buttons and added a button click event

ButtonClick(object sender,eventargs e)

Im sending all the Buttons created 1 to 3
I dont know how to get which button was clicked


thanks
 
P

Peter Duniho

Hi hows it going

ive made dynamic buttons and added a button click event

ButtonClick(object sender,eventargs e)

Im sending all the Buttons created 1 to 3
I dont know how to get which button was clicked

The "sender" parameter is a reference to the control instance that
generated the click. If you want the Button itself, you can just cast
that to the Button type, or if all you need is something from the base
Control class (e.g. the Tag property, commonly used to associate data with
a particular control), just cast it to the Control type.

You can even just compare the reference to your class variables that
reference each button (e.g. "button1", "button2", etc.). IMHO this is the
least preferable way to handle special-case behavior for a given button in
a shared handler, but for small numbers of buttons it's not actually all
that bad.

Finally, it probably goes without saying, but if you have drastically
different behavior in the handler depending on which button is clicked,
it's better to just have a different handler for each button.

Pete
 
A

Analizer1

There is no Work in the handler..just figure out what
button was clicked set a public property to the button number and close the
window..

its just a generic message box

thanks alot for the answer
 
P

Peter Duniho

There is no Work in the handler..just figure out what
button was clicked set a public property to the button number and close
the
window..

That's a self-contradictory statement. First, if there's no work in the
handler, why have it?

But more significantly, the steps "figure out what button was clicked",
"set a public property to the button number", and "close the window" all
represent some work. So, assuming your handler does those things, there
is definitely "work" in the handler.
its just a generic message box

The answer would be the same, no matter what kind of form the button is
contained in.
thanks alot for the answer

You're welcome.

Pete
 
A

Analizer1

I have to set a property so when the window closes
the programmer using the class knows what button was clicked
 

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