C#如何在单击一个按钮时启动计数器,然后在另一个按钮单击时重新启动并递增相同的计数器?

时间:2017-07-31 11:53:48

标签: c#

按钮3_click我需要我的计数器从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);  
    }

我正在尝试模拟短信打字。按钮2上有ABC,按钮3上有DEF。如果我在button2上点击一次,我应该得到A,双击给我B等。 如果我在button2上单击一次,在button3上单击一次,我得到的是AE而不是AD。每个按钮的计数器都很复杂,我更喜欢这种方式。谢谢。 :)

2 个答案:

答案 0 :(得分:1)

在SetLetter之后,你需要重置计数器,

SetLetter(b);
btncntr=0;

答案 1 :(得分:0)

这应该做到....

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);  
}