none
VB.NET: How to create a "countdown" counter RRS feed

  • Question

  • Hi,

    I need a counter that loops the countdown every 30 seconds.  I mean, I set something like if the value of this textbox > 1000, then start the countdown from 30 to zero every 30 seconds...until I click a stop button. I already have a Timer that has tick interval at 1000.

    Appreciate any assistance.


    Marilyn Gambone

    Thursday, October 31, 2019 3:19 PM

Answers

  • Hi,

    Is this effect that you want?

    Is so, you can try my code as follows:

    Public Class Form1
        Dim FreshTime As Integer = 30
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Static i As Integer = FreshTime
            TextBox2.Text = "Current countdown number:" & i
            TextBox1.Text = CInt(TextBox1.Text) - i
            If i = 0 Then
                i = FreshTime
            Else
                i -= 1
            End If
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = 1030
            Timer1.Interval = 1000
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Enabled = False
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If CInt(TextBox1.Text) > 1000 Then
                Timer1.Enabled = True
            Else
                MsgBox(TextBox1.Text & "is not greater than 1000")
                Return
            End If
        End Sub
    End Class
    

    Hope it be helpful.

    Best Regards,

    Julie


    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.

    • Marked as answer by deskcheck1 Sunday, November 3, 2019 9:49 AM
    Friday, November 1, 2019 7:25 AM
    Moderator

All replies

  • Please show your current code so we know what has and has not been tried.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 31, 2019 3:52 PM
    Moderator
  • Hi,

    Is this effect that you want?

    Is so, you can try my code as follows:

    Public Class Form1
        Dim FreshTime As Integer = 30
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Static i As Integer = FreshTime
            TextBox2.Text = "Current countdown number:" & i
            TextBox1.Text = CInt(TextBox1.Text) - i
            If i = 0 Then
                i = FreshTime
            Else
                i -= 1
            End If
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = 1030
            Timer1.Interval = 1000
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Enabled = False
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If CInt(TextBox1.Text) > 1000 Then
                Timer1.Enabled = True
            Else
                MsgBox(TextBox1.Text & "is not greater than 1000")
                Return
            End If
        End Sub
    End Class
    

    Hope it be helpful.

    Best Regards,

    Julie


    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.

    • Marked as answer by deskcheck1 Sunday, November 3, 2019 9:49 AM
    Friday, November 1, 2019 7:25 AM
    Moderator
  • Thanks much, Julie.  This is what I want!  You ROCK!

    Truly appreciated.


    Marilyn Gambone


    • Edited by deskcheck1 Sunday, November 3, 2019 9:51 AM
    Sunday, November 3, 2019 9:50 AM