Solved

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

Posted on 2008-06-16
4
228 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

18 Experts available now in Live!

Get 1:1 Help Now