Hey guys, I'm working on a poker clock for fun just for me and some friends, and have a form with 2 textboxes and a button. The user enters a time in minutes in the 2nd textbox, and presses the button, and it displays the time in Hours:Minutes:Seconds format, centered in the first textbox. That works fine, now I just need to know how to make the time in the first textbox start counting down. I haven't done any programming in like 3 years, so I have no clue how to do the counting down part, but from looking at other posts, I think I need to use DateTime, but just unsure of how to do it. Here's the code I have so far:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ts1 As TimeSpan
        Dim temp As Double
        Dim s As String
        temp = Val(TextBox2.Text)

        ts1 = TimeSpan.FromMinutes(temp)
        s = ts1.ToString()

        TextBox1.Text = s

    End Sub

P.S. I plan to use a timer, and enter the code for the countdown in the tick event. Any help is appreciated, thanks

Yes you should use a timer with 1000 ms interval.
So in every tick event you should lower the time in textbox1.

Right, I know that, but how do I lower it when it's in Hours:Minutes:Seconds format? That's the part I need help with.

Hi, this will work for you.

Public Class Form1

    Dim time As DateTime = Now
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = time.ToLongTimeString
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        time = time.AddSeconds(-1)
        TextBox1.Text = time.ToLongTimeString
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Start()
    End Sub
End Class

That gets the current time and starts counting down, which is not what I need. The time in the first textbox is the round time, which you get from the second textbox, and pressing the button. So, if I enter 15 in the second textbox, and press the button, the first textbox has 00:15:00 in it. Now I need that to start counting down until it reaches zero.

I think you were very close martonx, I just need to figure out how to use this part time = time.AddSeconds(-1) for my value after it's entered.

I don't like to make everything, but check this code:

Dim time As DateTime

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        time = time.AddMinutes(CDbl(TextBox1.Text))
        TextBox2.Text = time.ToLongTimeString
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        time = time.AddSeconds(-1)
        TextBox2.Text = time.ToLongTimeString
    End Sub

Nvm, it works, kindof. But it displays AM/PM, and adds 12 hours everytime, and I don't want that, it's just a countdown timer, it's not actually telling the time of day. How can I remove the AM/PM from it, and remove the 12 hours?

Update: I deleted your code, and tried using timespans instead, and got it to work, but I can't get it to loop properly, it just removes once second, here's what I have in the Timer1_Tick

Dim ts2, ts3, ts4 As TimeSpan
        Dim s2,s3 As String
        Dim q, q2 As Double

        Timer1.Enabled = True
        Timer1.Start()

        s2 = TextBox1.Text
     
        q = "1"
        q2 = Val(TextBox2.Text)


        If s2 <> "00:00:00" Then
            ts2 = TimeSpan.FromSeconds(q)
            ts3 = TimeSpan.FromMinutes(q2)
            ts4 = ts3.Subtract(ts2)

            s3 = ts4.ToString
            TextBox1.Text = s3

        ElseIf s2 = "00:00:00" Then
            Timer1.Stop()
            Timer1.Enabled = False
        End If

OK, I decided to start over and change strategies, and use string, and substrings and concatenation, which should make it easier for me to subtract from minutes and seconds. Now the problem I'm having is once the seconds and minutes get below 10, they don't have a leading zero, and the substring length of 2 I use is no longer valid, and I get an error saying "the index and length must refer to a place in the string". I tried adding a leading zero beforehand, but it can never get to that code because of the substring error. I'll give an example of my code in a minute.

EDIT: nvm, think i solved it, just need to test real quick

I have been looking to do the same thing. I have been using a really cool program for a while, but wanted to make my own for fun. What I have been using is from PokerRoom.com. It can be foundat this link

But I found some code that may be able to help you at this site here The author makes a timer that counts down, the only thing I am trying to figure out is how to incorporate minutes.

OK, solved the leading zeros, but now I once i reach 00 seconds left, it should subtract 1 minute, and make seconds 59 , but instead, does this: 15:0-1 . I'm using 15 minutes in this example, and once it reaches 15:00 it should go to 14:59 . Here's my new code that I'm having trouble with:

If secCat > "10" Then
            secCat -= 1
            TextBox1.Text = minCat & ":" & secCat
        ElseIf secCat = "10" Then
            secCat -= 1
            TextBox1.Text = minCat & ":" & "0" & secCat
        ElseIf secCat < 10 Then
            secCat -= 1
            TextBox1.Text = minCat & ":" & "0" & secCat
        ElseIf secCat = "00" Then
            secCat = "59"
            minCat -= 1
            TextBox1.Text = minCat & ":" & secCat
        ElseIf minCat = "10" And secCat = "00" Then
            minCat -= 1
            secCat = "59"
            TextBox1.Text = "0" & minCat & ":" & secCat
        ElseIf minCat < 10 And secCat = "00" Then
            minCat -= 1
            secCat = "59"
            TextBox1.Text = "0" & minCat & ":" & secCat
        ElseIf minCat = "00" And secCat = "00" Then
            Timer1.Stop()
            Exit Sub
        Else
            TextBox1.Text = minCat & ":" & secCat
        End If

Forgot, these are my variables :

tempStr = TextBox1.Text
minCat = tempStr.Substring(0, 2)
secCat = tempStr.Substring(3, 2)

OK, finally solved it, forgot to put the minute and second values into variables, once I added:

checkSec = Val(secCat)
        checkMin = Val(minCat)

and replaced minCat and secCat with checkMin and checkSec, I got it to work. Now I just need to do some minor tweaking, and will post code when done.

Here's the final working if-statement for mine, I hope this helps someone:

If checkSec = 0 And checkMin > 10 Then
            checkSec = "59"
            checkMin -= 1
            TextBox1.Text = checkMin & ":" & checkSec
            TextBox5.Text = "sec = 0"
        ElseIf checkSec = 0 And checkMin <= 10 Then
            checkSec = "59"
            checkMin -= 1
            TextBox1.Text = "0" & checkMin & ":" & checkSec
            TextBox5.Text = "sec = 0"
        ElseIf checkSec > 10 And checkMin > 10 Then
            checkSec -= 1
            TextBox1.Text = checkMin & ":" & checkSec
            TextBox5.Text = "sec > 10"
        ElseIf checkSec > 10 And checkMin = 10 Then
            checkSec -= 1
            TextBox1.Text = checkMin & ":" & checkSec
            TextBox5.Text = "sec > 10"
        ElseIf checkSec > 10 And checkMin < 10 Then
            checkSec -= 1
            TextBox1.Text = "0" & checkMin & ":" & checkSec
            TextBox5.Text = "sec > 10"
        ElseIf checkSec = 10 And checkMin > 10 Then
            checkSec -= 1
            TextBox1.Text = checkMin & ":" & "0" & checkSec
            TextBox5.Text = "sec = 10"
        ElseIf checkSec = 10 And checkMin = 10 Then
            checkSec -= 1
            TextBox1.Text = checkMin & ":" & "0" & checkSec
            TextBox5.Text = "sec = 10"
        ElseIf checkSec = 10 And checkMin < 10 Then
            checkSec -= 1
            TextBox1.Text = "0" & checkMin & ":" & "0" & checkSec
            TextBox5.Text = "sec = 10"
        ElseIf checkSec < 10 And checkMin > 10 Then
            checkSec -= 1
            TextBox1.Text = checkMin & ":" & checkSec
            TextBox5.Text = "sec = 10"
        ElseIf checkSec < 10 And checkMin = 10 Then
            checkSec -= 1
            TextBox1.Text = checkMin & ":" & "0" & checkSec
            TextBox5.Text = "sec = 10"
        ElseIf checkSec < 10 And checkMin < 10 Then
            checkSec -= 1
            TextBox1.Text = "0" & checkMin & ":" & "0" & checkSec
            TextBox5.Text = "sec = 10"
        ElseIf checkMin = 0 And checkSec = 0 Then
            Timer1.Stop()
            Exit Sub
        End If

Here is what I figured out in messing with it today.

Public Class Form1

    Dim time As DateTime

  

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        time = time.AddMinutes(CDbl(TextBox1.Text))
        time = time.AddHours(CDbl(TextBox2.Text))


        
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        time = time.AddSeconds(-1)

        TextBox3.Text = Format(time.Hour, "00")
        TextBox4.Text = Format(time.Minute, "00")
        TextBox5.Text = Format(time.Second, "00")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Form2.Show()
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Stop()
    End Sub


End Class

Check out a pic of my finished product :P I may release this if anyone wants it, let me know.

Also, I made one typo in the last if-statement i posted, here's the correction:

ElseIf checkSec < 10 And checkMin > 10 Then
            checkSec -= 1
            TextBox1.Text = checkMin & ":" & "0" & checkSec
            TextBox5.Text = "sec = 10"

The TextBox5 part was just a test I did to make sure the code was executing properly in each section.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.