文本框中的 VB.NET 实时时钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20037384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:17:46  来源:igfitidea点击:

VB.NET realtime clock in a textbox

vb.nettimetextboxclockform-load

提问by user2782104

How can I make it so that when I load the form a textbox will show me (dd/mm/yyy hh:mm:ss) format clock that is actually moving and is synced with the system?

我怎样才能让它在我加载表单时文本框会显示我 (dd/mm/yyy hh:mm:ss) 格式的时钟,它实际上正在移动并与系统同步?

I tried googling it but so far couldn't find anything that works. Most answers dealt with making labels into clocks but I figured it's the same with textboxes and tried doing what they said with no results. It shows me the time but it's just the time when the form loaded not an actual moving clock. I think most of the answers I found on google are dealing with older versions of VB that's why I can't get it to work.

我尝试使用谷歌搜索它,但到目前为止找不到任何有用的东西。大多数答案都涉及将标签制作成时钟,但我认为文本框也是如此,并尝试按照他们所说的去做但没有结果。它向我展示了时间,但这只是表单加载而不是实际移动时钟的时间。我认为我在 google 上找到的大多数答案都在处理旧版本的 VB,这就是我无法让它工作的原因。

P.S. I'm just learning coding so the simpler the code the better. Many step by step (like I'm 5) comments are appreciated as well. Thank You

PS我只是在学习编码,所以代码越简单越好。许多分步(如我是 5)评论也受到赞赏。谢谢你

采纳答案by Malcolm Salvador

Add a Timer to your form, and add this code to it's tick event.

将计时器添加到您的表单,并将此代码添加到它的滴答事件。

 Textbox1.text = Format(Now, "yyyy-MM-dd hh:mm:ss")

You now have a textbox which tells you the current date and time. Don't forget to enable your timer, though!

您现在有一个文本框,它告诉您当前的日期和时间。不过,不要忘记启用您的计时器!

回答by Reu Roo

Try to put your time and date string inside a timer. this is how it looks like:

尝试将您的时间和日期字符串放入计时器中。这是它的样子:

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) 处理 Timer1.Tick

    TimerText.Text = TimeString
    DaterText.Text = DateString

End Sub

结束子

It will surely show you a moving/real time clock that was sync to your computer. :)

它肯定会向您显示与您的计算机同步的移动/实时时钟。:)