我正在尝试创建一个票务柜台,基本上当用户点击按钮即。创建票证,计数器将递增1,直到第二天票证计数器将再次重置为0。
我已将计数器存储在数据库中,因此数据库知道特定用户的最后一个计数器。但是,一旦用户第二天点击按钮,我怎么能将计数器重置为0。我有一个名为checkformidnight的函数,如下所示,但不认为这是正确的方法。
Private Function CreatTicketNumber(ByVal officerId As Integer) As String
Dim counter As Integer = officer.Officer_Ticket_Counter '' from database
If IsMidnight(Now) Then
counter = 0
Else
counter = counter + 1
End If
Dim TicketNumber As String = counter.ToString("00")
Return TicketNumber
End Function
Public Function IsMidnight(ByVal value As Date) As Boolean
Return value.TimeOfDay.Ticks = 0
End Function
答案 0 :(得分:0)
在基本模式中,您可以这样做:
1)在设置(例如)中存储应用程序LOAD的日期。这样,您就拥有了当前日期:
My.Settings.LoadDate = Datetime.Now.Tostring
My.Settings.Save
2)使用BackgroundWorker并将以下代码放在:
中Do
Dim LastDate as datetime = cdate(my.settings.LoadDate)
if datetime.now.date > LastDate
Counter = 0
My.Settings.LoadDate = Datetime.Now.Tostring
My.Settings.Save
end if
Sleep(1200)
3)这样,您将有一个不同的线程,每1.2秒检查一次当前日期。如果日期过了今天,您将重置日期设置并重置您的计数器。
4)此代码可能已经过优化,但基本上可以解决您的问题。
答案 1 :(得分:0)
定义一个检查DateTime.Now的计时器,每当组件更改时,重置计数器
Dim time As DateTime = DateTime.Now
Dim count = 0L
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'you can use settings to store and restore data
If File.Exists("date") Then time = DateTime.FromBinary(Long.Parse(File.ReadAllText("date")))
If File.Exists("count") Then count = Long.Parse(File.ReadAllText("count"))
Dim t = New Timer
t.Interval = 1000 ' one second
t.Enabled = True
t.Start()
AddHandler t.Tick, Sub(ss As Object, ee As EventArgs)
If DateTime.Now.Day > time.Day Then
count = 0
End If
time = DateTime.Now
End Sub
AddHandler FormClosing, Sub(ss As Object, ee As FormClosingEventArgs)
File.WriteAllText("date", time.Ticks.ToString())
File.WriteAllText("count", count.ToString())
End Sub
End Sub
答案 2 :(得分:0)
将DateTime.Today.DayOfWeek
值与您的计数器一起存储在表单级别。在Form的Load()事件中,设置该变量。现在,只要单击按钮,就会检查它并相应地重置:
Public Class Form1
Private i As Integer = 0
Private LastDayOfWeek As DayOfWeek
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
LastDayOfWeek = DateTime.Today.DayOfWeek
Label1.Text = i
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If DateTime.Today.DayOfWeek = LastDayOfWeek Then
i = i + 1
Else
i = 0
LastDayOfWeek = DateTime.Today.DayOfWeek
End If
Label1.Text = i
End Sub
End Class