我希望计时器在表单上运行,该表单的值来自数据库。例如,当我将计时器的值存储在变量中时,我将其乘以60000以将毫秒转换为分钟。这是代码:
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim x as integer
details = ds.Tables(0).Rows(0).Item("Time")
Timer1.Interval =details * 60000
Timer1.Enabled = True
End Sub()
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
seconds.Text = x.ToString
If x= 0 Then
Timer1.Enabled = False
MessageBox.Show("You didn't finish in time.", "Sorry")
Me.Close()
Else
x-= 1
End If
End Sub
当我运行此代码时,计时器在后台运行,当时间到了,表单关闭。但是,当计时器正在滴答时,它不会在标签秒中显示。我希望你明白我的意思:)。
答案 0 :(得分:2)
如果您希望标签将秒数勾选为0,则无论您希望多长时间运行,您都需要定时器间隔为1000。
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
details = ds.Tables(0).Rows(0).Item("Time")
Timer1.Interval =1000
endTime = DateTime.Now.AddMinutes(details)
Timer1.Enabled = True
End Sub()
Dim endTime As DateTime
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If DateTime.Now > endTime Then
Timer1.Enabled = False
MessageBox.Show("You didn't finish in time.", "Sorry")
Me.Close()
Else
Seconds.Text = (DateTime.Now - endTime).ToString("hh\:mm\:ss")
End If
End Sub
答案 1 :(得分:0)
这是因为您的计时器以Minutes
间隔计时。如果你想在标签中显示秒数,那么你的计时器应该在1秒的时间间隔内打勾(可能是几毫秒等)。
因此,不要设置Timer1.Interval =details * 60000
,而是将x = details * 60
和Timer1.Interval
设置为1000。
您的代码也存在variable scoping
问题。您已在Form1_Load
中将x声明为局部变量,这意味着它在Timer1_Tick
内不可用。将其移到程序之外。
Dim x As Integer
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Details = ds.Tables(0).Rows(0).Item("Time")
Timer1.Interval = 1000
x = Details * 60
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
seconds.Text = x.ToString
If x = 0 Then
Timer1.Enabled = False
MessageBox.Show("You didn't finish in time.", "Sorry")
Me.Close()
Else
x-= 1
End If
End Sub
<强>已更新强>
这假设您的数据库保存值以毫秒为单位。否则适当修改。
Dim ticker As TimeSpan
Public Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Details = ds.Tables(0).Rows(0).Item("Time")
Timer1.Interval = 1000
ticker = TimeSpan.FromMilliseconds(Details)
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
seconds.Text = String.Format("{0:00}:{1:00}:{2:00}", ticker.Hours, ticker.Minutes, ticker.Seconds)
If ticker.Ticks <= 0 Then
Timer1.Enabled = False
MessageBox.Show("You didn't finish in time.", "Sorry")
Me.Close()
Else
ticker = ticker.Subtract(TimeSpan.FromSeconds(1))
End If
End Sub
答案 2 :(得分:0)
这可能不完全是你想要的,因为我没有将时间或任何东西转换为可能的基本时间和那种你应该能够从中工作的方式:)
Public Class Form1
Dim Time As Integer = 100 'Your time in seconds goes here!
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblTime.Text = Time
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Time = Time - 1
lblTime.Text = Time
If Time = 0 Then
Timer1.Enabled = False
MsgBox("You didn't finish in time.", "Sorry")
Me.Close()
End If
End Sub
End Class
任何问题只是给我留言