locked
I am trying to make a timer and the code sometimes work and sometimes it doesnt RRS feed

  • Question

  • Public Class Form1

        Private Property Timer As Object

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Not IsNumeric(TextBox1.Text) Then
                MsgBox("Please put in a number")
            Else
                Timer1.Enabled = True
                Timer1.Interval = 1000
                Time.text = TextBox1.Text
            End If
        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Timer.Text = 0 Then
                Timer1.Enabled = False
                MsgBox("time's up")
            Else
                TextBox1.Text = TextBox1.Text - 1

            End If
        End Sub



        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            TextBox1.Text = ""

        End Sub



        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Me.Close()
        End Sub

        Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

        End Sub
    End Class
    Wednesday, September 23, 2020 6:51 AM

All replies

  • Try removing this line:

       Private Property Timer As Object

    Replace

       If Timer.Text = 0 Then

    with

       If TextBox1.Text = 0 Then

    Also clarify if Time.text is correct in this line:

       Time.text = TextBox1.Text


    • Edited by Viorel_MVP Wednesday, September 23, 2020 8:20 AM
    Wednesday, September 23, 2020 8:17 AM
  • Hi harvey21,
    Based on your code. what is "Time" in this line: "Time.text = TextBox1.Text"?
    You can use textBox to time directly instead of Timer.Text.
    And what is your function of "Button3"?
    If you want to click button 3 to reset the timer, you can use Timer1.Stop() method.
    Here is my test code you can refer to.
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not IsNumeric(TextBox1.Text) Then
            MsgBox("Please put in a number")
        Else
            Timer1.Enabled = True
            Timer1.Interval = 1000
        End If
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
         Timer1.Stop()
         TextBox1.Text = ""
    End Sub
    
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If TextBox1.Text = 0 Then
            Timer1.Enabled = False
            MsgBox("time's up")
        Else
            TextBox1.Text = TextBox1.Text - 1
    
        End If
    End Sub
    The test result:
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 24, 2020 2:48 AM