如何设置DateTimePicker自动更新?
我已在表单中添加了日期时间选择器。最初,其值设置为数据库服务器日期和时间。设置其值后,我需要它自动更新自身,以便它始终显示数据库服务器日期和时间。我该怎么做? 我必须开发自定义控件吗?
提前致谢!
I've added a date time picker to a form. Initially its value is set to the DB sever date and time. Once its value is set, I need it to automatically update itself so that it always shows the DB server Date and time. How can I do this?
Do I have to develop a custom control?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以设置一个 System.Windows.Forms.Timer,每 X 秒(200 毫秒、500 毫秒、1 秒……等等)触发一个超时事件,并在事件处理程序中将选择器的 DateTime 增加超时量。由于时间不精确,这将逐渐偏离数据库服务器少量。
或者,您可以设置相同的计时器,而不是增加选择器中的时间,您可以继续请求最新的数据库服务器时间并将选择器设置为该时间。
如果主要目的是维护外观,我会使用第一个选项,然后仅当我需要某种操作的最新时间时才重新请求数据库时间。
You could set up a System.Windows.Forms.Timer that fires a timeout event every X seconds (200ms,500ms,1sec.. whatever) and in the event handler increment the DateTime of the picker by the timeout amount. This will gradually stray away from the DB server by small amounts because of inexact timing.
Alternatively you could set up the same timer instead of incrementing the time in your picker, you can keep requesting the latest DB server time and setting the picker to that.
If the main purpose is to maintain appearances I would use the first option, and then re-request the DB time only when I need the most up to date time for an operation of some sort.