1

我正在尝试为工作构建一个休息跟踪器,我想获得一个按钮来在标签中显示当前时间。我尝试了多种解决方案,这就是我所取得的成就。

Sub AddButtonClick(sender As Object, e As EventArgs)        
        Dim Start as Integer
        System.DateTime.Now = Start
        total.Text = Start      
End Sub

当我这样做时,我得到属性“现在”是只读的错误。

4

4 回答 4

13

这里有几个问题:

  • 你的任务是错误的;您正在尝试分配一个值DateTime.Now而不是Start
  • DateTime.Now是 type 的值DateTime,not Integer,所以分配无论如何都不会工作
  • 无论如何都不需要Start变量;它没有用
  • total.Text是类型的属性String- 不是DateTime Integer

(其中一些只会在执行时出现,除非你有Option Strict,你真的应该这样做。)

你应该使用:

total.Text = DateTime.Now.ToString()

...如果您想要特定格式的结果,可能会指定文化和/或格式说明符。

于 2013-07-20T17:48:54.573 回答
10

试试这个.....

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
于 2014-09-07T15:56:44.187 回答
3

尝试

total.Text = DateTime.Now.ToString()

或者

Dim theDate As DateTime = System.DateTime.Now
total.Text = theDate.ToString()

当您尝试将 a放入其中时,您声明Start为a ,这是不可能的。IntegerDateTime

于 2013-07-20T17:48:12.040 回答
-1

使用Date.Now代替DateTime.Now

于 2018-03-08T21:03:55.153 回答