简体   繁体   English

计时器在VB.NET 2010中不起作用

[英]Timer not working in VB.NET 2010

Well, i'm making a timer (to be specific, it is an Internet Café Standalone Timer that is on Visual Basic 2010 with a target framework of .NET Framework 4.5 . 好吧,我正在制作一个计时器(具体来说,这是一个InternetCafé独立计时器 ,它位于Visual Basic 2010上,目标框架为.NET Framework 4.5

Now, there is a mini form that shows your: time start, time out, minutes (total minutes of your rent), and your time left. 现在,有一个迷你表格可以显示您的信息: time start, time out, minutes (total minutes of your rent), and your time left. I've managed to get the content of time start on my Main.vb , same as the time out, and the minutes (see the code reference later). 我已经成功地得到时间的开始对我的内容Main.vb ,一样的时间了,和分钟(以后看到代码参考)。 Now, i'm trying to get the time left that is constantly updating every minute. 现在,我正在尝试保留不断更新的时间。 Output is like: " 33 minutes left " 输出就像:“ 33 minutes left

Main.vb : Main.vb

        If TextBox1.Text = My.Settings.userpass Then
            MessageBox.Show("Enjoy!", "...")
            dtFuture = DateTime.Now.AddMinutes(min.Text).ToString("hh:mm:ss")
            Limited.currtime.Text = DateTime.Now.ToString("hh:mm:ss")
            Limited.timeout.Text = dtFuture
            Limited.minutes.Text = min.Text
            Limited.Timer1.Interval = 1
            Limited.Timer1.Start()
            Limited.Show()
            Me.Close()
        Else
            ...
        End If

Limited.vb (this is the form when you have limited time, not open time): Limited.vb (这是您的时间有限而不是开放时间的表格):

Public Class Limited
    Private Sub Timer1_Tick(sender As Object, e As EventArgs)
        Dim foo As New TimeSpan
        Dim dtfuture As New DateTime
        Dim timecurrent As TimeSpan = dtfuture.Subtract(DateTime.Now)
        Dim timeCheckIn As String = Format(timecurrent, "HH:mm:ss")
        dtfuture = DateTime.Now.AddMinutes(minutes.Text)

        If Timer1.Interval <> 100 Then Timer1.Interval = 100
        If DateTime.Now > dtfuture Then
            timelft.Text = "Time's up!"
            block.Show()
            Timer1.Stop()
            Me.Close()
        Else
            foo = dtfuture - DateTime.Now
            ts = TimeSpan.Parse(timeCheckIn)
            timelft.Text = String.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", _
                              foo.Hours, foo.Minutes, foo.Seconds, foo.Milliseconds)
        End If
    End Sub

    Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click

    End Sub
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Label3.Text = TimeOfDay
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' hide app in to the taskbar

    End Sub
End Class

Basically your dtfuture does not have the correct value in Timer1_Tick. 基本上,您的dtfuturedtfuture中没有正确的值。 I am not sure what ts means so you may need to modify that part yourself. 我不确定ts含义,因此您可能需要自行修改该部分。

Private Sub Timer1_Tick(sender As Object, e As EventArgs)
    Dim foo As New TimeSpan

    ' This is when time will be up.
    Dim dtfuture As DateTime = DateTime.Parse(timeout.Text)

    ' I have no idea what these variables do. Not sure what "ts" below means.       
    Dim timecurrent As TimeSpan = dtfuture.Subtract(DateTime.Now)
    Dim timeCheckIn As String = Format(timecurrent, "HH:mm:ss")

    ' The If in original code is unnecessary.
    Timer1.Interval = 100

    If DateTime.Now > dtfuture Then
        timelft.Text = "Time's up!"
        block.Show()
        Timer1.Stop()
        Me.Close()
    Else
        foo = dtfuture - DateTime.Now
        ts = TimeSpan.Parse(timeCheckIn)
        timelft.Text = String.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", _
                          foo.Hours, foo.Minutes, foo.Seconds, foo.Milliseconds)
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM