Solved

C# Check if a button is clicked

Posted on 2012-03-15
4
488 Views
Last Modified: 2012-03-20
is there a legitimate way to see if a button is clicked in C#?
I have already tried if(this.button1.enable==true) but that doesn't work in my code. I have to check a couple of buttons in each IF Statement.
Please Help!

Paul
0
Comment
Question by:pauledwardian
4 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37727999
Checked how? Why wouldn't you handle the Click event of the button? The sender parameter would tell you which button was clicked (assuming you hooked multiple buttons up to the same handler function).

private void button1_Click(object sender, System.EventArgs e)
{
    MessageBox.Show(string.Format("'{0}' button was clicked!", (sender as Button).Name));
}

Open in new window

0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37728126
Simple, When ever the button gets clicked its handler gets called.
For each button have a flag(boolean) in your code and set it to true when ever the button gets clicked.

private bool IsButton1,IsButton2;

private void button1_Click(object sender, System.EventArgs e)
{
    IsButton1=true;
}

private void button2_Click(object sender, System.EventArgs e)
{
    IsButton2=true;
}

Open in new window

0
 
LVL 6

Expert Comment

by:effes
ID: 37730323
If you are going for something like a state button, you should use a CheckBox and set its Appearance to Button.
0
 

Author Closing Comment

by:pauledwardian
ID: 37745833
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

20 Experts available now in Live!

Get 1:1 Help Now