获取当前时间以显示在标签中。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

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

Getting Current time to display in Label. VB.net

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.Nowinstead of Start
  • DateTime.Nowis a value of type DateTime, not Integer, so the assignment wouldn't work anyway
  • There's no need to have the Startvariable anyway; it's doing no good
  • total.Textis a property of type String- not DateTimeorInteger
  • 你的任务是错误的;您正在尝试为其分配一个值DateTime.Now而不是Start
  • DateTime.Now是 type 的值DateTime, not Integer,因此无论如何分配都不起作用
  • Start无论如何都不需要变量;它没有用
  • total.Text是类型的属性String- 不是DateTimeInteger

(Some of these would only show up at execution time unless you have Option Stricton, 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 Startas an Integer, while you are trying to put a DateTimein it, which is not possible.

您声明Start为 an Integer,而您试图将 aDateTime放入其中,这是不可能的。

回答by silentXcoder

Use Date.Nowinstead of DateTime.Now

使用Date.Now代替DateTime.Now