Solved

Knowing what buttons was clicked in a particulat function in C#

Posted on 2008-06-16
4
229 Views
Last Modified: 2013-12-17
Hello,

I have a single function that will do a similar thing many times, but I need it to depend on the particular button that was pressed. I have 16 buttons, and their click event all goes to this single function. Is there a way for the function to know what button was pressed, and information about the button such as (most importantly) the text that is displayed.

I want to avoid having a function for each button that then calls the function that I want specifying what button is called (for example, theStandardFunction("button12");).

Using Visual C# Express.

Thanks.
0
Comment
Question by:dh-s
4 Comments
 
LVL 15

Accepted Solution

by:
crisco96 earned 200 total points
ID: 21792692
You can use the sender parameter which will be a reference to the button that called the method.

For instance:
((Button)sender).Text will give you the text of the button that was pressed.
0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 150 total points
ID: 21792759
You should use the Name property to identify the control via its ID. The Text property may not be unique.

Control oCtrl = (Control)sender;
switch ( oCtrl.Name )
{
   ... do something
}
0
 
LVL 21

Assisted Solution

by:naspinski
naspinski earned 150 total points
ID: 21792928
i think what alb66 meant was
Button clicked = (Button)sender;

string clickedID = clicked.ID; //ID instead of name
 

//or, all in one

string clickedID = ((Button)sender).ID;

Open in new window

0
 

Author Comment

by:dh-s
ID: 21793340
Thanks, they were all a great help!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now