简体   繁体   中英

How to allocate two different functions to the same button?

I am working on Winforms with C#.

I have a problem with the logic, there are two different methods that I need to call, so that if I click the button, the first action should get applied and if I click the same button again, the second action should get applied.

This is not the exact code but I have an idea something like this:

private void button1_Click(object sender, EventArgs e)
{
    if(button1.click==true)
    {
        fileNumber = 1;
        ImgSave();
    }
    else
    {
        ImgSave.exit();
    }
}

Here I have two problems regarding whether the button is already clicked:

  • If it's not clicked the Imgsave() should get activated.

  • If button is clicked the Imgsave() should get closed.

Can anyone please help me with this? Thanks.

You need to keep state somewhere. You can do this:


private bool buttonClicked = false;

private void button1_Click(object sender, EventArgs e)
{
    if(!buttonClicked)
    {
        buttonClicked = true;
        fileNumber = 1;
        ImgSave();
    }
    else
    {
        ImgSave.exit();
    }
}

This assumes you never going to click it a third time. If you are, you would need to handle that in some way.

I'd have either a class level variable track the number of times a button is clicked:

private bool _unclicked = false;
private void button1_Click(object sender, EventArgs e)
{
    if(!_unclicked)
    {
        _unclicked = true; //toggle so next time the ELSE will be performed
        fileNumber = 1;
        ImgSave();
    }
    else
    {
        _unclicked = false; //toggle it off again
        ImgSave.exit();
    }
}

, or I'd store it in the .Tag of the button:

private void button1_Click(object sender, EventArgs e)
{
    if(!button1.Tag.ToString() == "unclicked")
    {
        button1.Tag = "clicked"; //toggle so next time the ELSE will be performed
        fileNumber = 1;
        ImgSave();
    }
    else
    {
        button1.Tag = "unclicked"; //toggle it off again
        ImgSave.exit();
    }
}

You could also remove one event handler and add another:

private void button1_FirstClick(object sender, EventArgs e)
{
    button1.Clicked -= button1_FirstClick;             
    button1.Clicked += button1_SecondClick;             
    fileNumber = 1;
    ImgSave();
}

private void button1_SecondClick(object sender, EventArgs e)
{
    button1.Clicked -= button1_SecondClick;             
    button1.Clicked += button1_FirstClick;             
    ImgSave.exit();
}

I've always been less of a fan of adding and removing event handlers to achieve things like this but it's quite a clean solution

You should save your state in a variable. Your state will change after first click and you can change the state of Clicking button with calling ConditionChanger() method anytime.

For example you may need change the state of variable when you clicked a second button.

private void ConditionChanger(){
    myState = !myState;
}

Your variable :

private bool myState = false;

And your click event :

private void button1_Click(object sender, EventArgs e)
{
    if(!myState)
    {
        myState = true;
        fileNumber = 1;
        ImgSave();
    }
    else
    {
        ImgSave.exit();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM