Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you find out which button on a form was click?

Status
Not open for further replies.

bucca

Programmer
Sep 9, 2003
12
GB
Hi,

I am relatively new to C# and struggling to solve the following, does anyone know the answer or the location of any good reference material?

My problem is: How do you find out which button on a form was clicked, (programmatically in C#).

Any help much appreciated.

Cheers.
 
Each button has its own "Click" event. So if I had Button1 on my form, the event that would fire is

Button1_Click

Go to the form view in the IDE and double-click on the button in question. It should take you to a code window in the appropriate event.

Ben
A programmer was drowning. Lots of people watched but did nothing. They couldn't understand why he yelled "F1!"
 
Each control has a unique hash code. What I like to do is delegate all button_clicks to a single procedure/class method.

Then I can use "sender.GetHashCode()" to determine which button was clicked and branch to the appropriate code.

This approach is nice because on a web form with multiple buttons, there is almost always common code. No matter what button was clicked, do the common code, then switch case based on hash code.

Thomas D. Greer
 
Thanks for the help!

Thomas - The approach you describe is what I am attempting to achieve… but I still have a problem – Each time I click a button, the value returned from sender.GetHashCode() is different (I'm clicking the same button several times). I guess I'm missing something – Any ideas?

Thanks again.
 
Solved it by comparing the values each time - Sorry my logic was wrong when i wrote the last post (To early in the morning...)

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top