获取当前时间以显示在标签中。VB.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17764951/
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
Getting Current time to display in Label. VB.net
提问by user2602702
I'm trying to build a Break tracker for work and I would like to get a button to display the Current Time in a label. I have tried multiple solutions and this is how far I have gotten.
我正在尝试构建一个用于工作的 Break 跟踪器,并且我想要一个按钮来在标签中显示当前时间。我尝试了多种解决方案,这就是我得到的程度。
Sub AddButtonClick(sender As Object, e As EventArgs)
Dim Start as Integer
System.DateTime.Now = Start
total.Text = Start
End Sub
When I do this, I get the error that Property 'Now' is read only.
当我这样做时,我收到属性“现在”为只读的错误。
回答by Jon Skeet
There are several problems here:
这里有几个问题:
- Your assignment is the wrong way round; you're trying to assign a value to
DateTime.Now
instead ofStart
DateTime.Now
is a value of typeDateTime
, notInteger
, so the assignment wouldn't work anyway- There's no need to have the
Start
variable anyway; it's doing no good total.Text
is a property of typeString
- notDateTime
orInteger
- 你的任务是错误的;您正在尝试为其分配一个值
DateTime.Now
而不是Start
DateTime.Now
是 type 的值DateTime
, notInteger
,因此无论如何分配都不起作用Start
无论如何都不需要变量;它没有用total.Text
是类型的属性String
- 不是DateTime
或Integer
(Some of these would only show up at execution time unless you have Option Strict
on, which you really should.)
(其中一些只会在执行时出现,除非你已经Option Strict
打开了,你真的应该这样做。)
You should use:
你应该使用:
total.Text = DateTime.Now.ToString()
... possibly specifying a culture and/or format specifier if you want the result in a particular format.
...如果您想要特定格式的结果,可能会指定文化和/或格式说明符。
回答by edCoder
Try This...
尝试这个...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label12.Text = TimeOfDay.ToString("h:mm:ss tt")
End Sub
回答by Thousand
try
尝试
total.Text = DateTime.Now.ToString()
or
或者
Dim theDate As DateTime = System.DateTime.Now
total.Text = theDate.ToString()
You declare Start
as an Integer
, while you are trying to put a DateTime
in it, which is not possible.
您声明Start
为 an Integer
,而您试图将 aDateTime
放入其中,这是不可能的。
回答by silentXcoder
Use Date.Now
instead of DateTime.Now
使用Date.Now
代替DateTime.Now