如何在vb.net中运行此计时器?

如何在vb.net中运行此计时器?,vb.net,timer,Vb.net,Timer,我想要一个计时器在一个窗体上运行,该窗体的值是从数据库派生的。例如,当我将计时器的值存储在一个变量中时,我将其乘以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 * 60

我想要一个计时器在一个窗体上运行,该窗体的值是从数据库派生的。例如,当我将计时器的值存储在一个变量中时,我将其乘以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,你需要的计时器间隔是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秒的间隔内(可能是几毫秒等)滴答作响

因此,将
x=details*60
Timer1.Interval
设置为1000,而不是设置
Timer1.Interval=details*60000

您的代码也存在
变量作用域问题。您已在
Form1\u Load
中声明x为局部变量,这意味着它在
Timer1\u 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
TimeSpan时变暗股票代码
Public Sub Form1_Load(ByVal sender作为对象,ByVal e作为EventArgs)处理MyBase.Load
详细信息=ds.表格(0).行(0).项目(“时间”)
计时器1.间隔=1000
ticker=TimeSpan.From毫秒(详细信息)
Timer1.Enabled=True
端接头
私有子Timer1_Tick(ByVal发送方作为对象,ByVal e作为事件args)处理Timer1.Tick
seconds.Text=String.Format(“{0:00}:{1:00}:{2:00}”,ticker.Hours,ticker.Minutes,ticker.seconds)

如果ticker.Ticks这可能不完全是您想要的,因为我没有转换时间或任何东西,我只是尽可能地将时间转换为基本时间,这样您就可以从它开始工作:)


任何问题只需向我发送消息

滴答声仅每60秒发生一次,因此在上面的代码中,标签每60秒更新一次。他编写的计时器没有在分钟间隔内滴答声。当时间用完时,它只会滴答一次。@Pradeep Kumar:谢谢你。。。它随着时间的流逝而工作。但是,我怎样才能让它更人性化,或者更好看,而不是让秒滴答滴答地响个不停。@JoelCoehoorn:这正是我想要的happening@user3046786:如果需要,您可以将
x
的值转换为
Timer1\u Tick
事件中的HH:MM:SS格式,以显示在标签中。@PradeepKumar:sorry。。你能告诉我如何将x的值转换成HH:MM:ssh吗谢谢。。。它随着时间的流逝而工作。但是,为了简单起见,我建议使用
Stopwatch
对象来跟踪总的运行时间,而不是秒滴答声的大值,如何使它更易于用户使用,或者看起来更好。@user3046786,要以不同的格式格式化文本,请不要使用
TotalSeconds.ToString
。使用
TimeSpan
对象的
ToString
方法,可以指定格式。有关
TimeSpan
格式的详细信息,请参阅。@user3046786我更新了答案以使用更好的输出格式
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