DateTimerPicker控件是一个可以显示时间的控件,使用起来也比较简单,重要的属性大家可以参考下面这两篇文章
WinForm时间选择控件(DateTimePicker)如何选择(显示)时分秒
C# WinForm中DateTimePicker控件的Text属性和Value属性
这里我们使用DateTimePicker来实现一个系统时钟的功能
1.界面布局
界面布局如下
这里有1个DateTimePicker,两个Button用来显示text和value的区别,另外我们需要动态刷新,所以这里还需要一个Timer控件
2.用法示例
代码也比较简单,timer启动后,定时更新内容即可
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class FormMain : Form
{
public FormMain(ArrayList arrip_list)
{
InitializeComponent();
//设置定时器间隔为100ms,并启动定时器
timer1.Enabled = true;
timer1.Interval = 100;
timer1.Start();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dateTimePicker1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(dateTimePicker1.Value.ToString("yyyy-MM-dd"));
}
private void timer1_Tick(object sender, EventArgs e)
{
//更新时间
dateTimePicker1.Value = DateTime.Now;
/*
DateTime d = new DateTime(
DateTime.Now.Year,
DateTime.Now.Month,
DateTime.Now.Day,
DateTime.Now.Hour,
DateTime.Now.Minute,
DateTime.Now.Second,
Convert.ToInt32(DateTime.Now.DayOfWeek)//枚举转化为int
);
dateTimePicker1.Value = d;
*/
}
}
}
这里有个小问题就是,刷新频率和系统本身的刷新频率不一样,所以只能加快刷新间隔,但是这样又会比较浪费资源,暂时不知道有什么好的方法,能让两个刷新频率一样,希望各位看官指正,谢谢