为winform添加一个计数器类

时间:2012-12-04 04:06:57

标签: c# windows class

我有一个想要

的项目
  • 返回当前计数的方法。
  • 将计数设置为零的构造函数。

我有前几个但需要帮助返回计数为0然后构造函数。我需要通过添加一个计数器类来做到这一点,但我对添加它的方式感到困惑。 有人可以帮助我吗?

using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms;

namespace Project10TC 
{
    public partial class Form1 : Form    
    {
        int zero = 0;
        int i = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private EventHandler myCounter;

        // end of Form class

        private class myCounter()
        {
             myCounter = new myCounter( );
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Teancum Clark\nCS 1400\n Project 10");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = (++i).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = (--i).ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = (zero).ToString();

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:2)

public class Counter
{
    public int Value { get; private set; }

    public void Increment() 
    {
       Value = Value + 1;
    }

    public void Decrement() 
    {
      if (Value > 0) Value = Value - 1;
    }

    public Counter()
    {
       Value = 0;
    }
 }