【问题标题】:Add to int each time a button is clicked and show in textbox c#每次单击按钮时添加到 int 并显示在文本框中 c#
【发布时间】:2013-02-07 10:22:53
【问题描述】:

我对编程很陌生,每次单击按钮时,我都会尝试这样做,它会在int 的值上加一,并将其​​显示在文本框中。我的代码是:

private void button1_Click(object sender, EventArgs e)
{
    int a = 100;
    a++;

    txtBox1.Text = a.ToString();
}

所以当我点击它显示在文本框 101 中的按钮时,但是当我再次点击它时,我希望文本框显示 102 和 103 等。有什么想法吗?我认为这很容易并且使用了循环的一些变体,但是我尝试了一些东西,但似乎没有任何效果。任何提示将非常感谢!谢谢。

【问题讨论】:

    标签: c# textbox int add


    【解决方案1】:

    你必须将你的值存储在方法体之外。

    private int a = 100;
    
    private void button1_Click(object sender, EventArgs e)
    {        
         a++;
    
          txtBox1.Text = a.ToString();
    }
    

    您在程序中所做的是,每当您单击按钮时,新整数 a 被声明为值为 100,然后您将其增加 1,这就是您总是看到“101”的原因。

    【讨论】:

    • 非常感谢!!现在看起来很简单! :)
    【解决方案2】:

    在您的代码中,您每次单击按钮时都会忽略 a 并一遍又一遍地为其赋值。

    你应该在button1_Click方法之外声明变量:

    class Window1
    {
        int a = 100;
        ....
        private void button1_Click(object sender, EventArgs e)
            {
                a++;
                txtBox1.Text = a.ToString();
            }
    }
    

    【讨论】:

      【解决方案3】:

      您需要将a 声明为包含您的方法的类的成员:

      private int _a = 100;
      
      private void button1_Click(object sender, EventArgs e)
      {
          _a++;
      
          txtBox1.Text = _a.ToString();
      }
      

      如果您不这样做,则每次单击按钮时都会有一个新实例,因此您将始终在文本框中看到 101。

      【讨论】:

        【解决方案4】:

        可以不创建全局字段并在文本框中存储点击次数。 如果您有多个按钮,这将特别方便。

        private void button1_Click(object sender, EventArgs e)
            {     
                if (txtBox1.Tag is int)
                {
                    int a = (int)txtBox1.Tag;
                    a++;
        
                    txtBox1.Tag = a;
        
                    txtBox1.Text = a.ToString();
                }
                else
                {
                    txtBox1.Tag = 100;
                    txtBox1.Text = 100;
                }
        }
        

        【讨论】:

          【解决方案5】:
          int a = 100;
          txtBox1.Text = a.ToString();
            ......
          

          private void button1_Click(object sender, EventArgs e) {

              a++;
          
             txtBox1.Text = a.ToString();
          }
          

          【讨论】:

            【解决方案6】:

            在每次执行时间函数时,将int a = 100; 放在button1_Click(object sender, EventArgs e) 中会将a 设置为100。如果您需要一个计数器,请将其放在函数之外(然后它将只初始化一次。)并在执行函数时将其递增。

            解决方案

            int a = 100;
            private void button1_Click(object sender, EventArgs e)
            {        
                a++;
            
                txtBox1.Text = a.ToString();
            }
            

            【讨论】:

              【解决方案7】:
              static int a = 100;
              
              private void button1_Click(object sender, EventArgs e)
              {        
                   a++;
              
                    txtBox1.Text = a.ToString();
              }
              

              【讨论】:

                【解决方案8】:

                如果你想优化你的代码而不是首先设置文本框属性text = 100并在按钮点击事件中只写一行代码

                private void button1_Click(object sender, EventArgs e)
                {            
                    txtBox1.Text = (Convert.ToInt32(txtBox1.Text) + 1).ToString();
                }
                

                如您所知,C# 逐行编译代码,您只需一行代码即可提供更快的性能。

                【讨论】:

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