【问题标题】:reset button for a simple counter简单计数器的重置按钮
【发布时间】:2014-10-17 11:51:48
【问题描述】:

我正在使用 Visual Studios 2010 在 c# 中为 windows 7.1 制作一个非常简单的计数器应用程序。

我添加了两个按钮,一个 + 和一个 -(用于加法或减法)。然后将结果打印到一个文本块中,一切正常。

现在我正在尝试添加一个重置按钮以将文本块中的值放回零,我正在努力。我可以使用 Tostring 将其设为 0,但计数器值不会重置为零!

我意识到这是一个非常容易解决的问题,但我一无所获!谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;


namespace PhoneApp1
{

public partial class MainPage : PhoneApplicationPage
{
    int counter = 0;

    // Constructor
    public MainPage()
    {

        InitializeComponent();

    }

    private void add_Tap(object sender, GestureEventArgs e)
    {
        counter++;
        number.Text = counter.ToString();
    }

    private void subtract_Tap(object sender, GestureEventArgs e)
    {
        counter--;
        number.Text = counter.ToString();
    }

    private void button1_Tap(object sender, GestureEventArgs e)
    {
        //need to make clicking this button reset number(textblock) to 0
    }

}

}

【问题讨论】:

    标签: c# .net wpf counter


    【解决方案1】:
    private void button1_Tap(object sender, GestureEventArgs e)
    {
        counter = 0;
        number.Text = counter.ToString();        
    }
    

    你只需给 TextBox 和 counter 你想要设置的数字。

    不能在counter 变量上使用ToString() 的原因是因为计数器是integer 而不是string,而ToString() 返回一个字符串。

    【讨论】:

      【解决方案2】:
       private void button1_Tap(object sender, GestureEventArgs e)
      {
          counter = 0;
          number.Text = counter.ToString();
      }
      

      【讨论】:

        猜你喜欢
        • 2017-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-29
        • 2020-10-03
        • 1970-01-01
        • 2021-10-08
        相关资源
        最近更新 更多