You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows窗体中DateTimePicker实现实时走时(秒动态更新)的方案求助

WinForms DateTimePicker 实现实时跳动的时间显示

要让DateTimePicker显示实时跳动的秒数,核心是每秒更新一次控件的时间值——DateTimePicker本身不会自动刷新时间,因此需要借助Timer控件来实现定时更新,具体步骤如下:

  1. 在窗体中添加Timer控件(从工具箱拖拽即可),设置其Interval属性为1000(代表1秒触发一次),并启用该控件。
  2. 编写Timer的Tick事件处理逻辑,每秒将DateTimePicker的Value更新为当前时间。

完整代码示例

初始化控件(可放在窗体构造函数或Load事件中)

// 配置DateTimePicker仅显示时间
dtpTimeIn.Format = DateTimePickerFormat.Time;
dtpTimeIn.Value = DateTime.Now;
dtpTimeIn.ShowUpDown = true;

// 配置Timer
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Tick += Timer1_Tick;

Timer的Tick事件处理方法

private void Timer1_Tick(object sender, EventArgs e)
{
    // 每秒更新为当前时间,实现秒数跳动
    dtpTimeIn.Value = DateTime.Now;
}

可选:窗体关闭时清理Timer

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    timer1.Enabled = false;
    timer1.Dispose();
}

说明

你之前的代码仅在初始化时设置了一次当前时间,后续不会自动更新。通过Timer每秒触发一次时间更新,就能让DateTimePicker的秒数实时跳动了。

内容的提问来源于stack exchange,提问作者Kuya Carlyy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 08:47:22