control questions

  • Thread starter Thread starter Analizer1
  • Start date Start date
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
 
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
 
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
 
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
 
I have to set a property so when the window closes
the programmer using the class knows what button was clicked
 
Back
Top