Solved

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

Posted on 2008-06-16
4
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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