AndjelaS
AndjelaS

Reputation: 3

C# How to start counter on one button click, then restart and increment the same counter on another button click?

On button3_click I need my counter to start counting from 0.

        static int btncntr=0;
        private void button2_Click(object sender, EventArgs e)
    {
         btncntr++;
         timer1.Stop();
         timer1.Start();
         string a = GetLetter(2);
         char b = char.Parse(a);
         SetLetter(b);
    }
         private void button3_Click(object sender, EventArgs e)
    {
          btncntr++;
          timer1.Stop();
          timer1.Start();
          string a = GetLetter(3);
          char b = char.Parse(a);
          SetLetter(b);  
    }

I am trying to simulate SMS typing. On button2 there's ABC, on button 3 there's DEF. If i click once on button2 I should get A, double click gives me B etc. If I click once on button2 and once on button3 what I get is AE instead AD. It would be complicated having counter for each button and I prefer it this way. Thanks. :)

Upvotes: 0

Views: 463

Answers (2)

ajg
ajg

Reputation: 1753

This should do it....

static int btncntr=0;
static int lastButton=0;

private void button2_Click(object sender, EventArgs e)
{
    //if the last click wasnt button 2 then reset
    if(lastButton != 2)
        btncntr = 0;

    //save last button clicked
    lastButton = 2;

    btncntr++;
    timer1.Stop();
    timer1.Start();
    string a = GetLetter(2);
    char b = char.Parse(a);
    SetLetter(b);
}

private void button3_Click(object sender, EventArgs e)
{
    //if the last click wasnt button 3 then reset
    if(lastButton != 3)
        btncntr = 0;

    //save last button clicked
    lastButton = 3;

     btncntr++;
     timer1.Stop();
     timer1.Start();
     string a = GetLetter(3);
     char b = char.Parse(a);
     SetLetter(b);  
}

Upvotes: 0

raichiks
raichiks

Reputation: 286

after SetLetter, you need to reset counter,

SetLetter(b);
btncntr=0;

Upvotes: 1

Related Questions