Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to make increment and decrement counter. There are two buttons called X and Y. If first press X and then press Y counter should increment. If first press Y and then press X counter should decrement.

I am not familiar with c#. So can anyone help me please ?? :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
200 views
Welcome To Ask or Share your Answers For Others

1 Answer

Sounds like you need 2 variables: a counter, and the last button pressed. I'll assume this is a WinForms application, since you did not specify at the time I am writing this.

class MyForm : Form
{
    // From the designer code:
    Button btnX;
    Button btnY;

    void InitializeComponent()
    {
        ...
        btnX.Clicked += btnX_Clicked;
        btnY.Clicked += btnY_Clicked;
        ...
    }

    Button btnLastPressed = null;
    int counter = 0;

    void btnX_Clicked(object source, EventArgs e)
    {
        if (btnLastPressed == btnY)
        {
            // button Y was pressed first, so decrement the counter
            --counter;
            // reset the state for the next button press
            btnLastPressed = null;
        }
        else
        {
            btnLastPressed = btnX;
        }
    }

    void btnY_Clicked(object source, EventArgs e)
    {
        if (btnLastPressed == btnX)
        {
            // button X was pressed first, so increment the counter
            ++counter;
            // reset the state for the next button press
            btnLastPressed = null;
        }
        else
        {
            btnLastPressed = btnY;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...