Solved

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

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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