写了个小程序,就是在窗体上显示系统实时时间
现在是运行的时候,显示的是系统当前的时间
并不实时
请问怎么才能实时呢

解决方案 »

  1.   

    timer的“Interval”属性设为“1000”
    每个这么长时间刷新一次就行了
      

  2.   


    this.Clock.Enabled=true;
                this.Clock.Interval=1000;
                this.Clock.AutoReset = true;
                this.Clock.SynchronizingObject=this;
                this.Clock.Elapsed+=new System.Timers.ElapsedEventHandler(Clock_Elapsed);这样么、
    我都设了呀
    就是不实时
    气死了
      

  3.   

    private void button1_Click(object sender, EventArgs e)
            {
                this.timer1.Interval = 1000;//设置每隔一秒钟执行一次
                this.timer1.Enabled = true;
                this.timer1.Start();
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                this.Text = DataTime.Now.ToString();        }
      

  4.   

     private void timer1_Tick(object sender, EventArgs e)
      {
           this.Text = DataTime.Now.ToString();
           this.Refresh();//刷新一下
      }
      

  5.   

    Timer timer1;
      this.timer1.Interval = 1000;
      this.timer1.Tick += new System.EventHandler(this.timer1_Tick);  private void timer1_Tick(object sender, EventArgs e)
      {
      timebox.Text = DateTime.Now.ToString();  
      }
      

  6.   

    Clock.start()是让时钟启动,开始计时。
    timer1.Enable=true;
    可以在设计界面改变这个属性。
      

  7.   

    你上哪给我找个Clock出来啊 有吗!!!??
      

  8.   

    我还是把代码直接贴上来吧、、
    我真是没办法了
    private void InitializeComponent()
            {
                this.lbTime = new System.Windows.Forms.Label();
                this.Clock=new System.Timers.Timer();
                this.SuspendLayout();            //
                //lbTime
                //
                this.lbTime.Font=new System.Drawing.Font("Arial",42F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((System.Byte)(0)));
                this.lbTime.Name="lbTime";
                this.lbTime.Size=new System.Drawing.Size(300,130);
                this.lbTime.TabIndex=0;
                //this.lbTime.Text="dd:dd:dd";
                this.lbTime.Text = GetTime();
                this.lbTime.TextAlign=System.Drawing.ContentAlignment.MiddleCenter;
                //
                //Clock
                //
                this.Clock.Enabled=true;
                this.Clock.Interval=1000;
                this.Clock.AutoReset = true;
                this.Clock.SynchronizingObject=this;
                this.Clock.Start();
                this.Clock.Elapsed+=new System.Timers.ElapsedEventHandler(Clock_Elapsed);
                
                //  
                // Form1
                // 
                this.AutoScaleBaseSize=new System.Drawing.Size(6,14);
                //this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 131);
                this.Controls.AddRange(new System.Windows.Forms.Control[]{
                this.lbTime});
                this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.Fixed3D;
                this.MaximizeBox=false;
                this.Name = "Form1";
                this.Text = "时钟";
                //this.Load += new System.EventHandler(this.Form1_Load);
                this.StartPosition=System.Windows.Forms.FormStartPosition.CenterScreen;
                ((System.ComponentModel.ISupportInitialize)(this.Clock)).EndInit();
                this.ResumeLayout(false);        }
            #endregion        void Clock_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                //throw new Exception("The method or operation is not implemented.");
                //if (sender == Clock)
                //{
                //this.lbTime.Text = System.DateTime.Now.ToString();
                this.lbTime.Text = GetTime();
                this.Refresh();
                //}
            }
            public string GetTime()
            {
                string TimeInString = "HH:mm:ss";
                //int hour = DataTime.Now.Hour;有错
                int hour = System.DateTime.Now.Hour;
                int min = System.DateTime.Now.Minute;
                int sec = System.DateTime.Now.Second;            TimeInString = (hour < 10) ? "0" + hour.ToString() : hour.ToString();
                TimeInString += ":" + ((min < 10) ? "0" + min.ToString() : min.ToString());
                TimeInString += ":" + ((sec < 10) ? "0" + sec.ToString() : sec.ToString());
                return TimeInString;
            }
           
        }
    }
      

  9.   

    private void timer1_Tick(object sender, EventArgs e)
    {
    label1.Text = DataTime.Now.ToString();
    }
    界面里面拖个Timer控件就可以了,属性设置里面Enable=true;Interval=1000
    ok,绝对没问题
      

  10.   

    this.Clock.Start();好歹也在放在this.Clock.Elapsed+=new System.Timers.ElapsedEventHandler(Clock_Elapsed); 后执行最好放在form1_Load里执行getTime方法最好这么写  public string GetTime()
      {
      return Datetime.Now.toString("HH:mm:ss");
      }
      

  11.   


            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Interval = 1000;
                timer1.Start();
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                TimeSpan ts = new TimeSpan(Environment.TickCount * TimeSpan.TicksPerMillisecond);
                string TempStr = string.Format("系统已经运行时间:{0:d2}天 {1:d2}时 {2:d2}分 {3:d2}秒", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);            DateTime dt = DateTime.Now;
                string TempStr2 = DateTime.Now.ToString("    系统当前时间:yyyy/MM/dd HH:mm:ss");            this.Text = TempStr + "   " + TempStr2;
            }
      

  12.   

    原来楼主用的是System.Timers下的。
    那就必须要在Form_Load中Clock.Start();
    另外GetTime()方法写得太复杂。一句就可以 return DateTime.Now.ToLongTimeString();
      

  13.   

    1000每隔一秒就会执行tick事件
      

  14.   

    在工具箱里,拖入一个label控件,双击控件。
    在方法里面写代码如下:
    label1.Text = DataTime.Now.ToString();
      

  15.   

            private void Form1_Load(object sender, EventArgs e)
            {
                TimerManage()
            }        private void TimerManage()
            {
                int Ti = 1;
                this.timer1.Interval = Ti * 1000;
                this.timer1.Tick -= new EventHandler(SendManage);
                this.timer1.Tick += new EventHandler(SendManage);
                this.timer1.Enabled = true;
            }        private void SendManage(object sender, EventArgs e)
            {
               label1.Text = DataTime.Now.ToString();
            }
      

  16.   

    private void button1_Click(object sender, EventArgs e)
    {    System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 1000;
        timer1.Start();}
    void timer1_Tick(object sender, EventArgs e)
    {
        button1.Text = DateTime.Now.ToString();
    }
      

  17.   

    你用System.Timers.Timer timer是无法访问窗体上的控件的,请使用System.Windows.Forms.Timer timer1 
      

  18.   

    你先得获取服务器时间而不是本地时间,然后用TIMER做就可以了
    SELECT CONVERT(CHAR(19),GETDATE(),121),要具体的时间格式可以参照帮助文档
    或者使用API先同步服务器的时间到本地来,代码如下:
    public struct SYSTEMTIME
            {
                public ushort wYear;
                public ushort wMonth;
                public ushort wDayOfWeek;
                public ushort wDay;
                public ushort wHour;
                public ushort wMinute;
                public ushort wSecond;
                public ushort wMilliseconds;            /// <summary>
                /// 从System.DateTime转换。
                /// </summary>
                /// <param name="time">System.DateTime类型的时间。</param>
                public void FromDateTime(DateTime time)
                {
                    wYear = (ushort)time.Year;
                    wMonth = (ushort)time.Month;
                    wDayOfWeek = (ushort)time.DayOfWeek;
                    wDay = (ushort)time.Day;
                    wHour = (ushort)time.Hour;
                    wMinute = (ushort)time.Minute;
                    wSecond = (ushort)time.Second;
                    wMilliseconds = (ushort)time.Millisecond;
                }
                /// <summary>
                /// 转换为System.DateTime类型。
                /// </summary>
                /// <returns></returns>
                public DateTime ToDateTime()
                {
                    return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
                }
                /// <summary>
                /// 静态方法。转换为System.DateTime类型。
                /// </summary>
                /// <param name="time">SYSTEMTIME类型的时间。</param>
                /// <returns></returns>
                public static DateTime ToDateTime(SYSTEMTIME time)
                {
                    return time.ToDateTime();
                }
            }
            
            [DllImport("CoreDll.dll")]
            public static extern bool MessageBeep(BeepType beepType);        [DllImport("CoreDll.dll")]
            public static extern bool SetLocalTime(ref SYSTEMTIME Time);
            [DllImport("CoreDll.dll")]
            public static extern void GetLocalTime(ref SYSTEMTIME Time);
      

  19.   

    在工具栏里托一个TIMER空间到你的窗口上,默认的叫TIMER1 你给他改个你喜欢的名字,比如的NowTimer
    把他的属性中的Enable改成True,Interval 改成1000(或者可以100,程序效率低点,但是精确度高一点)
    然后在窗口上双击你的NowTimer创建它的
    void NowTimer_Tick(object sender, EventArgs e)
    {
    }
    函数,在里面写上
    Label.Text=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    当然Label是你要显示时间的标签控件的名字在你要开始计时的函数(比如Form_Load)里面写上NowTimer.Start()开始计时这样够简单具体吧
      

  20.   

    楼主是否将Enable属性设为true?
    protected void Page_Load(object sender, EventArgs e)
        {
           
            Label1.Text = Convert.ToString(System.DateTime.Now);
        }
      

  21.   

    用个timer控件,然后每秒读取系统时间
      

  22.   

    我照着做了  怎么会有这样的错误呢 ?
    当前上下文不存在Datatime呀???
      

  23.   

    你这样来用会比较好点:::::
    private void w_consume_Load(object sender, EventArgs e)
            {
                //获取系统时间
                label1.Text = "时间:" + DateTime.Now.ToString();
                //Timer timer1 = new Timer();
                timer1.Interval = 1000;
                timer1.Tick += new EventHandler(timer1_Tick);
                timer1.Start();        }        private void timer1_Tick(object sender, EventArgs e)
            {
                timer1.Enabled = true;
                label1.Text = "时间:" + DateTime.Now.ToString();
            }