【问题标题】:need implement C# Counter需要实现 C# Counter
【发布时间】:2009-08-30 14:01:10
【问题描述】:

我想制作增量和减量计数器。 有两个按钮称为 X 和 Y。如果先按 X,然后按 Y,计数器应该增加。 如果先按 Y 再按 X 计数器应该递减。

我不熟悉c#。所以有人可以帮我吗? :(

【问题讨论】:

  • 这是作业吗?如果没有,那么只有一个“增量”按钮和一个“减量”按钮不是更简单吗?如果是这样,到目前为止您尝试过什么?
  • 那是 ASP.NET、WPF 还是 WinForms 按钮?
  • 您想要 Web 应用程序还是桌面应用程序?您是在寻找完整的代码还是想要指向不同资源的指针(如何创建应用程序、如何执行逻辑、如何显示结果)?有没有你知道的编程语言,所以我们可以做类比?

标签: c# counter


【解决方案1】:

听起来您需要 2 个变量:一个计数器和最后按下的按钮。我假设这是一个 WinForms 应用程序,因为您在我写这篇文章时没有指定。

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

【讨论】:

    【解决方案2】:

    你会想要一个变量来跟踪计数器。

    int counter = 0;
    

    如果它是一个 Web 应用程序,那么您必须将其存储在某些位置,例如会话状态。 然后在您的增量计数器按钮中:

    counter++;
    

    并在您的递减计数器按钮中执行以下操作:

    counter--;
    

    【讨论】:

      【解决方案3】:

      在另一个网站上找到了这个:

      public partial class Form1 : Form
      {
              //create a global private integer 
              private int number;
      
              public Form1()
              {
                  InitializeComponent();
                  //Intialize the variable to 0
                  number = 0;
                  //Probably a good idea to intialize the label to 0 as well
                  numberLabel.Text = number.ToString();
              }
              private void Xbutton_Click(object sender, EventArgs e)
              {
                  //On a X Button click increment the number
                  number++;
                  //Update the label. Convert the number to a string
                  numberLabel.Text = number.ToString();
              }
              private void Ybutton_Click(object sender, EventArgs e)
              {
                  //If number is less than or equal to 0 pop up a message box
                  if (number <= 0)
                  {
                      MessageBox.Show("Cannot decrement anymore. Value will be  
                                                      negative");
                  }
                  else
                  {
                      //decrement the number
                      number--;
                      numberLabel.Text = number.ToString();
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多