簡體   English   中英

在一天中的指定時間向 vb.net 中的標簽顯示消息

[英]display a message to a label in vb.net at specified times during the day

我想在一天中的 5 個特定時間自動在文本框中顯示一個句子。 例如:在凌晨 5:30,
Textbox1.text = "早餐"
早上 7:30
textbox1.text = "去學校",
等等。計時器可以在應用程序啟動時啟動,盡管它需要參考當地時間或某個恆定時間,因為程序需要在一周中的每一天的同一時間輸出,而無需我手動更改它。

正確的做法是這樣的:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Set the interval at startup.
    Timer1.Interval = CInt(GetNextNotificationTime().Subtract(Date.Now).TotalMilliseconds)
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'Set the interval at each notification.
    Timer1.Interval = CInt(GetNextNotificationTime().Subtract(Date.Now).TotalMilliseconds)

    'Not sure whether this is required when the Interval changes or not.
    Timer1.Stop()
    Timer1.Start()

    'Do the work here.
End Sub

Private Function GetNextNotificationTime() As Date
    '...
End Function

如何實現GetNextNotificationTime方法取決於通知時間的存儲方式。 只有當通知到期時, Timer才會Tick

您仍然可以使用Timer執行此操作,而無需進行任何數學運算...

每次Timer引發Tick事件時,您都會檢查以下值: System.DateTime.Today.Now.ToString("HH:mm") 如果等於你預設的時間,修改TextBox的文字

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM