?
Solved

Windows c# app - many buttons, one function - how to tell which button was clicked

Posted on 2003-03-09
9
Medium Priority
?
203 Views
Last Modified: 2010-04-15
I have a windows forms app in vs.net / c#. I have 26 buttons - A-Z, rather than have 26 different handlers I would like to have one function then determine which button was pressed (Im going to pass the relevant letter into a stored procedure to a database).

The closest I have got is this:
MessageBox.Show(sender.ToString());

-that shows me whats in 'sender' - though as well as the text of the button I get:


Is there any way I can easily just get what I require?
0
Comment
Question by:niico
  • 5
  • 4
9 Comments
 

Author Comment

by:niico
ID: 8097614
sorry was supposed to read:

-that shows me whats in 'sender' - though as well as the text of the button I get:
System.Windows.Forms.Button, Text: A

0
 
LVL 10

Accepted Solution

by:
smegghead earned 200 total points
ID: 8098599
You have to cast 'sender' back into button.

so...

string ClickedButtonText=((Button)sender).Text;

0
 

Author Comment

by:niico
ID: 8098641
Thanks works well.

Im looking for a good book/reference - anything you could recommend?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 10

Expert Comment

by:smegghead
ID: 8098672
If you have a look on amazon you'll find lots of books, all with reviews (watch out for the ones that are too positive though.. they're probably written by the author ;-))
0
 

Author Comment

by:niico
ID: 8098692
actually i already have a few books from amazon - it can be hard to find what i need in them though not sure why.

thanks again...
0
 
LVL 10

Expert Comment

by:smegghead
ID: 8098719
Forums like this are a good place to learn.. in my opinion you pick up a lot more by getting your hands dirty and asking questions when you get stuck.

However, for complex subjects, it's always good to have some background knowledge so that you understand the basics.
0
 

Author Comment

by:niico
ID: 8098748
yes indeed - ive learnt a lot from places like this - it always makes me wonder how people learnt this stuff in the first place though :)

Ive got a couple of good books on c#.net but I cant find anything on the windows forms side...

where did you find out about what I asked in this question out of interest?
0
 
LVL 10

Expert Comment

by:smegghead
ID: 8098763
I learned about casting when writing c++ applications. It's the same in c# (as are most things)

http://www.intap.net/~drw/cpp/cpp03_03.htm

it's just a simple way of converting one variable type into another.
0
 

Author Comment

by:niico
ID: 8101745
ok thanks - so casting is basically just converting between datatypes... is there not a more c#ey ToString() type way of doing this as well? (just wondering)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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.
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

592 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