Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

C# Check if a button is clicked

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
pauledwardian
Asked:
pauledwardian
1 Solution
 
käµfm³d 👽Commented:
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
 
BuggyCoderCommented:
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
 
effesCommented:
If you are going for something like a state button, you should use a CheckBox and set its Appearance to Button.
0
 
pauledwardianAuthor Commented:
Thanks
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now