Solved

C# Check if a button is clicked

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hey!!!! 1 20
SQL Help 27 40
Example works but why do I need to click twice on button 4 36
Expression Evaluater 3 24
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 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