用label和timer控件来实现当前时间的“动态显示”,或者用其他方法也可以,代码框架如下:
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace labeltimer1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void timer1_Tick(object sender, EventArgs e)
{ }
}
}
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace labeltimer1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void timer1_Tick(object sender, EventArgs e)
{ }
}
}
private void Form1_Load(object sender, EventArgs e)
{
Timer timer = new Timer();
timer 过期事件 = (刷新label);
timer.过期时间 = 1秒;
} public void 刷新label()
{
label.Text = Datetime.Now;
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
} private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace labeltimer1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void timer1_Tick(object sender, EventArgs e)
{ } private void label1_Click(object sender, EventArgs e)
{
this.label1.Text = DateTime.Now.ToString();//这样是静态显示的
}
}
}
using System.Drawing;
using System.Text;
using System.Windows.Forms; namespace labeltimer1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void timer1_Tick(object sender, EventArgs e)
{
label.Text=DateTime.Now.ToString();
}
}
}你添加一个time控件 设置Enable是true
{
this.timer1 = new Timer();
timer1.Interval = 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Text = DateTime.Now.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Interval = 10000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
label.Text=DateTime.Now.ToString();
}
正解,将timer1的enable设置为true的话,就不用start()了
{
this.timer1.Interval = 1;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
}
最重要的一句,谢了!