复位按钮,用于简单的计数器

时间:2014-10-17 11:51:48

标签: c# .net wpf counter

我正在使用Visual Studio 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
    }

}

}

2 个答案:

答案 0 :(得分:1)

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

您只需为TextBox和计数器指定要设置的数字。

您无法在counter - 变量上使用ToString()的原因是,因为计数器为integer且没有string,而ToString()返回字符串。

答案 1 :(得分:0)

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