怎么显示实时系统时间 写了个小程序,就是在窗体上显示系统实时时间现在是运行的时候,显示的是系统当前的时间并不实时请问怎么才能实时呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 timer的“Interval”属性设为“1000”每个这么长时间刷新一次就行了 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);这样么、我都设了呀就是不实时气死了 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(); } private void timer1_Tick(object sender, EventArgs e) { this.Text = DataTime.Now.ToString(); this.Refresh();//刷新一下 } 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(); } Clock.start()是让时钟启动,开始计时。timer1.Enable=true;可以在设计界面改变这个属性。 你上哪给我找个Clock出来啊 有吗!!!?? 我还是把代码直接贴上来吧、、我真是没办法了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; } }} private void timer1_Tick(object sender, EventArgs e){label1.Text = DataTime.Now.ToString();}界面里面拖个Timer控件就可以了,属性设置里面Enable=true;Interval=1000ok,绝对没问题 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"); } 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; } 原来楼主用的是System.Timers下的。那就必须要在Form_Load中Clock.Start();另外GetTime()方法写得太复杂。一句就可以 return DateTime.Now.ToLongTimeString(); 1000每隔一秒就会执行tick事件 在工具箱里,拖入一个label控件,双击控件。在方法里面写代码如下:label1.Text = DataTime.Now.ToString(); 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(); } 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();} 你用System.Timers.Timer timer是无法访问窗体上的控件的,请使用System.Windows.Forms.Timer timer1 你先得获取服务器时间而不是本地时间,然后用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); 在工具栏里托一个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()开始计时这样够简单具体吧 楼主是否将Enable属性设为true?protected void Page_Load(object sender, EventArgs e) { Label1.Text = Convert.ToString(System.DateTime.Now); } 用个timer控件,然后每秒读取系统时间 我照着做了 怎么会有这样的错误呢 ?当前上下文不存在Datatime呀??? 你这样来用会比较好点:::::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(); } 有没有专门介绍算法的资源 如何访问C#中文件资源 移动网关什么意思。 我做了个简单的聊天的工具有些问题问问大家 C# 反射获取DLL窗口中控件的事件 如何编程修改从表中的数据。 关于触发器的问题 c#高级编程中文第3版谁有? 以前写php现在又要写.net,没有学过.net,应怎样学才好呢?看了两天的书,没有什么头绪。进来up有分. 急急急,在线等,高手请进,来者有分 xtragrid gridview主从表绑定右键菜单判断选择行的问题。。请大哥姐姐们指教! 高分求教C#如何实现排列输出
每个这么长时间刷新一次就行了
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);这样么、
我都设了呀
就是不实时
气死了
{
this.timer1.Interval = 1000;//设置每隔一秒钟执行一次
this.timer1.Enabled = true;
this.timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
this.Text = DataTime.Now.ToString(); }
{
this.Text = DataTime.Now.ToString();
this.Refresh();//刷新一下
}
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();
}
timer1.Enable=true;
可以在设计界面改变这个属性。
我真是没办法了
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;
}
}
}
{
label1.Text = DataTime.Now.ToString();
}
界面里面拖个Timer控件就可以了,属性设置里面Enable=true;Interval=1000
ok,绝对没问题
{
return Datetime.Now.toString("HH:mm:ss");
}
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;
}
那就必须要在Form_Load中Clock.Start();
另外GetTime()方法写得太复杂。一句就可以 return DateTime.Now.ToLongTimeString();
在方法里面写代码如下:
label1.Text = DataTime.Now.ToString();
{
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();
}
{ 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();
}
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);
把他的属性中的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()开始计时这样够简单具体吧
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Convert.ToString(System.DateTime.Now);
}
当前上下文不存在Datatime呀???
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();
}