我正在開發一個專案,其中用戶設定帶有資訊、日期和時間的提醒,通知應該彈出的時間,通知應該是可點擊的,并打開另一個帶有提醒資訊的表單。到目前為止,我可以設定提醒,但只要點擊提醒按鈕,它就會彈出,我的通知看起來是標準的 Windows 10 通知,我只想將通知安排在某個日期和時間。包括日期和時間在內的資訊保存在 Access 資料庫中。我正在使用 VB.Net
親切的問候
形式:
這是我目前的提醒表
這是我的通知的樣子
Imports System.Data.OleDb
Public Class frmReminder
Private CurrentReminderID As Integer = -1
Private Sub frmReminder_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BtnClear.PerformClick()
Timer1.Enabled = True
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
Label6.Text = ""
TxtCustName.Text = ""
TxtDeviceInfo.Text = ""
TxtPrice.Text = ""
TxtDateDue.ResetText()
End Sub
Private Sub BtnSetReminder_Click(sender As Object, e As EventArgs) Handles BtnSetReminder.Click
If DbConnect() Then
Dim SQLCmd As New OleDbCommand
If CurrentReminderID = -1 Then
With SQLCmd
.Connection = cn
.CommandText = "Insert into TblReminder (CustomerName, DeviceInfo, RepairPrice, ReminderDate)"
.CommandText &= "Values (@CustomerName, @DeviceInfo, @RepairPrice, @ReminderDate)"
.Parameters.AddWithValue("@CustomerName", TxtCustName.Text)
.Parameters.AddWithValue("@DeviceInfo", TxtDeviceInfo.Text)
.Parameters.AddWithValue("@RepairPrice", TxtPrice.Text)
.Parameters.AddWithValue(" @ReminderDate", TxtDateDue.Text)
.ExecuteNonQuery()
.CommandText = "Select @@Identity"
CurrentReminderID = .ExecuteScalar
Label6.Text = CurrentReminderID
End With
End If
End If
Notification.ShowBalloonTip(1000, "Reminder", "Customer Order Due!", ToolTipIcon.None)
End Sub
End Class
uj5u.com熱心網友回復:
您需要使用 Timer 并避免不斷讀取資料庫,您可以使用一些非常簡單的東西,例如 DateTimePicker 或 TextBox。在這個例子中,我將使用一個 Texbox。
因此,在您的表單中插入一個 TextBox1 并具有第一個日期/時間的值(僅從您的資料庫中讀取一次)。Textbox1 文本應該類似于“2021-10-21 15:00”(不要輸入秒數,不要忘記在日期和時間之間留一個空格)
TextBox1.Text="2021-10-21 15:00" ' read this data from your database
然后您需要在表單中插入一個 Timer 并添加以下值:
Timer1.Interval = 60000 ' 1 minute
Timer1.Enabled = True
最后,您需要雙擊 Timer1 并撰寫以下代碼:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If TextBox1.Text = Now.ToString("yyyy-MM-dd HH:mm") Then
' At this moment you should read the next date/time and put that in TextBox1
MsgBox("Show the popup notification")
End If
End Sub
uj5u.com熱心網友回復:
由于您使用的是資料庫連接,因此假設您熟悉多執行緒。
規則 1. 最簡單的方法是讓您的應用程式跟蹤(在 RAM 中)下一個通知。任何時候你通過你的應用程式添加一個新的,它都會將它插入到資料庫中,并檢查它是否會在 RAM 中的當前一個之前過去。當 RAM 中的一個過期時,它會從資料庫中獲取下一個。總而言之,您找到了一種方法來確保在任何給定時間將下一個流逝的時間保存在 RAM 中。
規則 2. 然后,當通知被“保存”在 RAM 中時,比較通知應該顯示的時間 - 當前時間,這將為您提供通知應該顯示的時間,并為該時間設定一個計時器。
規則 3. 當計時器已經過去時,顯示 RAM 中的通知(根據規則 1,這是剛剛過去的那個)。并迭代 DB 以找到下一個到期。沖洗并重復。(請注意,當第一個被插入時,您需要處理邊緣情況,因為不會有任何比較,以及當最后一個被通知時,因為不會有任何通知可以存盤在 RAM 中)
Tada,抱歉我沒有代碼示例。但這就是我無數次解決這個問題的方式。它作業得很好。
編輯 1:代碼
'main for testing
Dim notificationScheduler As NotificationScheduler = NotificationScheduler.getInstance
notificationScheduler.insertNotification(New MyNotificationEntity("Yay, working", DateTime.Now.AddSeconds(15)))
' expected that the one below will be displayed since insertNotification will only keep track of the newest one in the queue
' As mentioned in your post, you have a DB to persist them long term and as such will only need one in the APP's RAM at a time
notificationScheduler.insertNotification(New MyNotificationEntity("Yay, working-2", DateTime.Now.AddSeconds(3)))
' myNotificationEntity, will be replaced by whatever class you are using to package your reminder's in a single class (dont see on in your code sample)
Public Class MyNotificationEntity
Public data As String
Public timeToShow As DateTime
' this is a dumb bucket class, you will not end up using this one but rather whatever class you use to contain your notifications
' you will need to add _timeToShow as a variable to your class or change the code in the NotificationScheduler to be compatible with your Notification class
Public Sub New(data As String, timeToShow As DateTime)
Me.data = data
Me.timeToShow = timeToShow
End Sub
End Class
'NotificationScheduler class
Public Class NotificationScheduler
Private Shared _instance As NotificationScheduler
Private Shared ReadOnly _lock As Object = New Object()
Dim queuedNotification As MyNotificationEntity
Dim WithEvents timer As System.Windows.Forms.Timer = New Timer
Dim timerElapsedTime As DateTime = DateTime.Now
Private Sub New()
timer.Enabled = False
End Sub
Public Shared Function getInstance()
' enforce singleton design pattern (i.e. there should only be one of these in existence)
' If this does not make sense, that is fine, at some point lookup the 'singleton design pattern'
' Also, SyncLock is a VB (.net really) element that means in a multi-threaded application only one of them at any given time is inside the SyncLock block,
' the rest are queued to alleviate race conditions. Again, if this does not make sense, lookup 'race conditions'
SyncLock _lock
If (_instance Is Nothing) Then
_instance = New NotificationScheduler()
End If
End SyncLock
Return _instance
End Function
' called after you have placed the new notification in your DB, replaces the currently queued notification only if the new one will be shown first
Public Sub insertNotification(notificationEntity As MyNotificationEntity)
If timerElapsedTime.Ticks < notificationEntity.timeToShow.Ticks Then
queuedNotification = notificationEntity
timer.Stop()
Dim span As TimeSpan = notificationEntity.timeToShow - timerElapsedTime
timer.Interval = CInt(span.TotalMilliseconds)
timer.Start()
End If
End Sub
Private Sub timer_elapsed() Handles timer.Tick
' the notification time has arrived
showNotification()
readNextFromDb()
End Sub
Private Sub readNextFromDb()
' read the next (if any) from the and handle edge cases for when there is not next one in the DB
'IMPORTANT: if the one you read from the DB has already happened (i.e. the notification dateTime is after now, notify and do this method again)
'' your code here
End Sub
Private Sub showNotification()
MsgBox(queuedNotification.data) ' do whatever notification you want
End Sub
End Class
You will need to (it appears) create a class NotificationDAO (Data Access Object) or NotificationDTO (Data Transfer Object) or a generic NotificationEntity. That class will have a constructor and public instance variables for the items you need to store about a notification (i.e. every column you have in the 'TblReminder ' table) and everything should work fairly easily.
Note, the above code does not work nor check for pulling notifications out of a DB that have already passed their notification time, read the comments in the code for further information.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/335617.html
上一篇:如何擴展任何型別的List類
