Solved

C# Check if a button is clicked

Posted on 2012-03-15
4
493 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 75

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A short film showing how OnPage and Connectwise integration works.

914 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

19 Experts available now in Live!

Get 1:1 Help Now