我写好了时钟程序在控制台中可以走动,就是做不成Form下的时钟程序,请高手指点
先给出控制台源码:DigitalTime类文件:
class MyDigitalTime
    {
        private int hour;    //(0-23);
        private int minute;  //(0-59);
        private int second;  //(0-59);        public MyDigitalTime()
        {
            SetHour(0);
            SetMinute(0);
            SetSecond(0);
        }        public  MyDigitalTime(int _hour,int _minute,int _second)
        {
            SetHour(_hour);
            SetMinute(_minute);
            SetSecond(_second);
        }        public void SetHour(int _hour)
        {
            if (_hour >= 0 && _hour < 24)
            {
                hour = _hour;
            }
            else
            {
                hour = 0;
            }
        }        public void SetMinute(int _minute)
        {          
            if (_minute >= 0 && _minute < 60)
            {
                minute = _minute;
            }
            else
            {
                minute = 0;
            }
        }        public void SetSecond(int _second)
        {
            if (_second >= 0 && _second < 60)
            {
                second = _second;
            }
            else
            {
                second = 0;
            }
        }        public int GetHour()
        {
            return hour;
        }        public int GetMinute()
        {
            return minute;
        }        public int GetSecond()
        {
            return second;
        }
        public void DisplayTime()
        {
            Console.Write("当前时间是  {0}:{1}:{2}",hour,minute,second);
        }        public void TickTime()
        {
            
            SetSecond((GetSecond() + 1) % 60);
            if (GetSecond() == 0)
            {
                SetHour((GetMinute() + 1) % 60);
                if (GetMinute() == 0)
                {
                    SetHour(GetHour() + 1 % 24);
                }
            }
        }        
        public void FlashDispaly()
        {            int oldtime = DateTime.Now.Second;
            while(true)
            {
               int newtime = DateTime.Now.Second;
               if ((newtime - 1) == oldtime)
                {
                    TickTime();
                    Console.Write("\r"); 
                    DisplayTime();
                    oldtime = newtime;
                }
                            }
        }}
主函数文件:
class Program
    {
        static void Main(string[] args)
        {
            MyDigitalTime mydt = new MyDigitalTime(10,30,00);
            mydt.DisplayTime();
            
            mydt.FlashDispaly();
        }
    }

解决方案 »

  1.   

    代码没有问题啊,只是在FORM中你程序的输出在"输出"窗口中.
    菜单"视图"--"其它窗口"--"输出",你就可以在下面的"输出"窗口中看到程序的输出了,
    不知道你想改成什么样的
      

  2.   

    用DeBug.WriterLine(); 
    显示在输出窗口中,菜单[调试]-->[输出]  调出输出窗口   
    MessageBox也可以. 
      

  3.   


    首先,指出你的程序里一个应该是笔误吧?SetHour((GetMinute() + 1) % 60);这里应为:
    SetMinute((GetMinute() + 1) % 60);吧? 你的程序我没有仔细看,应该是没有问题,但我运行到10:30:57秒的时候就不动了,不知道什么原因,我没有深究。
     
    其实,用WinForm不用这么麻烦,有很多东西WinForm已经做好了,比如说时间秒的变化,需在窗体里加一个 timer控件,并不需要你   
    public void FlashDispaly() 
            { 
    ......
                while(true) 
                { 
                 ......
                } 
            } 
    这一段。也不需要 public void TickTime()
            {            SetSecond((GetSecond() + 1) % 60);
     ......
            }里“+ 1”这一段,直接取系统时间。常规作法如下:放一个label,在加一个Timer控件 private void Form1_Load(object sender, EventArgs e)
            {
    timer1.Enabled = true;
                timer1.Interval = 1000;
                this.label1.Text = Convert.ToString(DateTime.Now.ToLocalTime());
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                
                this.label1.Text = Convert.ToString(DateTime.Now.ToLocalTime());
            } 
    就这么简单。最大限度的用你的程序,在你的程序的基础上修改,用Winform实现你的程序,如下:class MyDigitalTime修改下列地方:
     
    class MyDigitalTime
        {
            public static int hour;    //(0-23); 
            public static int minute;  //(0-59); 
            public static int second;  //(0-59);       public void FlashDispaly()
            {
                TickTime();
            }    }主窗口里加入如下代码:namespace ClockWinForm
    {
        public partial class ClockForm : Form
        {
    .......
            MyDigitalTime mydt = new MyDigitalTime(10, 30, 00);        private void ClockForm_Load(object sender, EventArgs e)
            {
                this.label1.Text = MyDigitalTime.hour.ToString() + ":" + MyDigitalTime.minute.ToString() + ":" + MyDigitalTime.second.ToString();
            }
            private void timer1_Tick_1(object sender, EventArgs e)
            {
                mydt.FlashDispaly();
                this.label1.Text = MyDigitalTime.hour.ToString() + ":" +  MyDigitalTime.minute.ToString() + ":" + MyDigitalTime.second.ToString();
            }
        }
    }       
      

  4.   

     private void ClockForm_Load应为:
    private void ClockForm_Load(object sender, EventArgs e) 
            { 
                timer1.Interval = 1000; //间隔时间1000毫秒=1秒
                this.label1.Text = MyDigitalTime.hour.ToString() + ":" + MyDigitalTime.minute.ToString() + ":" + MyDigitalTime.second.ToString(); 
            } 
      

  5.   

    private void ClockForm_Load应为: 
    private void ClockForm_Load(object sender, EventArgs e) 
            { 
                timer1.Interval = 1000; //间隔时间1000毫秒=1秒 
                  timer1.Enabled = true; 
                this.label1.Text = MyDigitalTime.hour.ToString() + ":" + MyDigitalTime.minute.ToString() + ":" + MyDigitalTime.second.ToString(); 
            } 
      

  6.   

    谢谢 HELLOWORDC!!!
    还是 不行,死机了
      

  7.   

    /*
    加一个 timer 控件,把控件自动运行时间设置成一秒钟运行一次这样就可以看到秒在增加的效果。
    */
     
    private void Form1_Load(object sender, EventArgs e)
            {
                this.timer1.Interval = 1000;
                this.timer1.Enabled = true;
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                this.label1.Text = System.DateTime.Now.ToLongDateString() + System.DateTime.Now.ToLongTimeString();
            }