Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-16
4
Medium Priority
?
234 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 800 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 600 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 600 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

Independent Software Vendors: 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

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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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