Hi Folks,

I am creating a GUI application and I've come to a situation where I need to check whether a button has been clicked.

So logically: ---If--- the "button" has been clicked -> Do this.

As soon as that button is clicked, it should jump to a method which has other commands.

I'm of course using Visual Studio 2008 - Visual studio creates pre-made methods for click events whenever you double click on a button in design mode. So at the moment the button method's signature looks like this:

private void btAdd_Click(object sender, EventArgs e)

At the moment, i've added something like:

if btAdd_Click() = true;

Do this

Which of course throws errors because btAdd_Click is a method and therefore needs some arguments. Which arguments, I don't know....


Please help - Much appreciated.

Usmaan~

Just put your Do This code inside the btAdd_Click() method. It will only execute when the button is clicked. There is no need to add a check anywhere in your code, the IDE has done that for you.

Edited 6 Years Ago by Momerath: n/a

Before I answer your question, I just want to cover a couple of things that you ought to know about this code:

if btAdd_Click() = true;

Firstly, if you are comparing objects you need to use the "equal to" operator which in C# is == . ie if(x == y) .
When you use a single '=' it is an assignment operator.
Very small difference in syntax that causes very big differences in behaviour.

Secondly, your are correct that btAdd_Click requires parameters to run so you can't call btAdd_Click() with empty parentheses. The only method signature for btAdd_Click requires two parameters, one of type object and one of type EventArgs.

In your code btAdd_Click(someObject, someEventArgs) == true would still be incorrect because btAdd_Click is declared with a void return. If you had the following method:

//method with return type bool
private bool CheckSomething()
{
    if(something)
        return true;
    else
        return false;
}

Then it is valid syntax to directly compare the methods return value:

if(CheckSomething() == true)
    //do something

Lastly, when dealing with boolean values it is not necesarry to use "== true".
Examine the following:

bool someFlag = true;
if(someFlag == true)
    //do something

Since someFlag is already a boolean, you are basically asking "if (true is true)" so you can use the boolean itself as the condition for the if statement:

if(someFlag)
    //do something

Along the same lines, you have the NOT operator, which in C# is ! . This returns the opposite of the boolean value.
For instance:

bool someFlag = true;
bool oppositeFlag = !someFlag //oppositeFlag will be false

if(!someFlag)
    //do this if someFlag is false

//this is the same as
if(someFlag == false)
    //do something

With that said; if you want to run some code when the button is clicked then Momerath is correct, you just put your code inside the btAdd_Click event handler method.
If you need to check at a later date if the button was clicked previously you could add a boolean flag:

public partial class Form1 : Form
{
    private bool btAddClicked = false;

    private void btAdd_Click(object sender, EventArgs e)
    {
         btAddClicked = true;
    }    
}

Then you can use btAddClicked == true to check later in your code. Just remember to set it back to false at the appropriate time :)

Edited 6 Years Ago by Geekitygeek: n/a

Comments
Um... Wow! :twisted:

LOL You flatter me :p There were a few months at the start of the year where I was too bogged down with work to give Daniweb much time (much to my dismay) but its still here :) Im just one tiny cog in the great machine that is our community ;)

By the way - I'm making a Calculator....and I'm just improvising on how to go about making it all work....

It's really difficult!

This article has been dead for over six months. Start a new discussion instead.