调用下面的类,代码没有错误.但是label1的时间只增加1秒,就停在那儿不动了啊!!(label1应该是计时的时间的显示)form1窗体中的代码如下:Timer类的代码在后面using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication36
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }
        int hour = 0; int minute = 0; int second = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
        
            Time time = new Time(hour, minute, second);
           
            time  ++;
            this.label1.Text = "时间在流逝:" + time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
        }        private void Form1_Load(object sender, EventArgs e)
        { 
       
        }        private void timer2_Tick(object sender, EventArgs e)        {
            DateTime dtime = DateTime.Now;
            int hour = dtime.Hour;
            int minute = dtime.Minute;
            int second = dtime.Second;
             this.label2.Text = "当前时间" + hour + ":" + minute + "" + ":" + second;        }
    }
}Timer类里面的代码如下:using System;
using System.Collections.Generic;
using System.Text;namespace WindowsApplication36
{
    class Time
    {        private int hours; //小时
        private int minutes; //分钟
        private int seconds; //秒钟        public Time()
        {
            this.hours = 0;
            this.minutes = 0;
            this.seconds = 0;
        }        public Time(int hours, int minutes, int seconds)
        {
            this.hours = hours;
            this.minutes = minutes;
            this.seconds = seconds;
        }        public void SetHours(int hours)
        {
            this.hours = hours;
        }        public void SetMinutes(int minutes)
        {
            this.minutes = minutes;
        }        public void SetSeconds(int seconds)
        {
            this.seconds = seconds;
        }        public int GetHours()
        {
            return this.hours;
        }        public int GetMinutes()
        {
            return this.minutes;
        }        public int GetSeconds()
        {
            return this.seconds;
        }        public static Time operator ++(Time time)
{
time.seconds++;
if (time.seconds >= 60)
{
time.minutes++;
time.seconds = 0;
if (time.minutes >= 60)
{
time.hours++;
time.minutes = 0;
time.seconds = 0;
if (time.hours >=24 )
{
time.hours = 0;
time.minutes = 0;
time.seconds = 0;
}
}
}
return new Time(time.hours,time.minutes,time.seconds);
}
    }
}

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication36
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
     
            }
    int hour = 0; int minute = 0; int second = 0;
    Time time =null;

            private void button1_Click(object sender, EventArgs e)
            {
                timer1.Enabled = true;
    time = new Time(hour, minute, second);
            }
            
            private void timer1_Tick(object sender, EventArgs e)
            {
            
               
               
                time  ++;
                this.label1.Text = "时间在流逝:" + time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
            }        private void Form1_Load(object sender, EventArgs e)
            { 
           
            }        private void timer2_Tick(object sender, EventArgs e)        {
                DateTime dtime = DateTime.Now;
                int hour = dtime.Hour;
                int minute = dtime.Minute;
                int second = dtime.Second;
                 this.label2.Text = "当前时间" + hour + ":" + minute + "" + ":" + second;        }
        }
    }
      

  2.   

    int hour = 0; int minute = 0; int second = 0; 
    Time time =null; 
            private void button1_Click(object sender, EventArgs e) 
            { 
                timer1.Enabled = true; 
    time = new Time(hour, minute, second); 
            } 
    修改这段代码后.问题没得到解决还多出一个警告
    警告 1 私有字段“WindowsApplication36.Form1.time”已被赋值,但从未使用过它的值 D:\My Documents\Visual Studio 2005\Projects\WindowsApplication36\WindowsApplication36\Form1.cs 23 14 WindowsApplication36
    楼上?
      

  3.   

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Text; 
    using System.Windows.Forms; namespace WindowsApplication36 

        public partial class Form1 : Form 
        { 
            public Form1() 
            { 
                InitializeComponent();         } 
    int hour = 0; int minute = 0; int second = 0; 
    Time time =new Time(); 
            private void button1_Click(object sender, EventArgs e) 
            { 
                timer1.Enabled = true; 
                time.SetMinutes(minute );
       
    ....等等
      你只出现一秒的的原因是 你每次都new 一个新time对象。所以他每次都只走一秒        } 
            
            private void timer1_Tick(object sender, EventArgs e) 
            { 
            
              
              
                time  ++; 
                this.label1.Text = "时间在流逝:" + time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds(); 
            }         private void Form1_Load(object sender, EventArgs e) 
            { 
          
            }         private void timer2_Tick(object sender, EventArgs e)         { 
                DateTime dtime = DateTime.Now; 
                int hour = dtime.Hour; 
                int minute = dtime.Minute; 
                int second = dtime.Second; 
                this.label2.Text = "当前时间" + hour + ":" + minute + "" + ":" + second;         } 
        } 

      

  4.   

    而他只是警告并不影响运行难道你就不能自己在想想吗
    比如在timer tick事件里判断time是不时null的
      

  5.   

    呵呵.自己搞错拉
    ericzhangbo1982111  
    代码是正确的啊...
    不好意思了啊