954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Timer But With Hours

This Code allows you too countdown seconds and minutes but im not sure how to add hours or maybe even days to it:(
===========================================================================================

Imports System.Threading.Thread

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If InStr(timetowait.Text, ":") Then
            Dim time() As String
            time = Split(timetowait.Text, ":", , CompareMethod.Text)
            countminutes.Text = time(0)
            countseconds.Text = time(1)
            Timer1.Interval = 1000
            Timer1.Start()
        Else
            MsgBox("You have to insert like this 1:10")
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim minutes As Integer = countminutes.Text
        Dim seconds As Integer = countseconds.Text
        If minutes = 0 And seconds = 0 Then
            MsgBox("Time Ended")
            Timer1.Stop()
        End If
        If seconds = 0 Then
            minutes -= 1
            seconds += 60
        End If
        seconds -= 1
        If minutes <> -1 Then
            countminutes.Text = minutes
            countseconds.Text = seconds
        End If
    End Sub
End Class

=========================================================================================if anyone can help me it would be appreciated:icon_cry:

VB 2012
Junior Poster
155 posts since Jul 2010
Reputation Points: 15
Solved Threads: 1
 

The easiest and savest way is this:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
		Dim tstan As TimeSpan
		If Not TimeSpan.TryParse(txtTime.Text, tstan) Then
			MsgBox("Please enter the time in format d:h:m:s")
			Return
		End If
		Timer1.Interval = 1000
		Timer1.Start()
	End Sub

	Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
		Dim tspan As TimeSpan = TimeSpan.Parse(txtTime.Text)
		If tspan.Days = 0 AndAlso tspan.Hours = 0 AndAlso tspan.Minutes = 0 AndAlso tspan.Seconds = 0 Then
			Timer1.Stop()
			Return
		End If
		txtTime.Text = tspan.Subtract(TimeSpan.FromSeconds(1)).ToString
	End Sub
GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

WOw thanks much better than thee old one
Im gona check on it now

VB 2012
Junior Poster
155 posts since Jul 2010
Reputation Points: 15
Solved Threads: 1
 

How would i beable to show Days , hours , minutes ,seconds in diffrent textboxes
When they count down ofcourse so tht each of them are seprate

VB 2012
Junior Poster
155 posts since Jul 2010
Reputation Points: 15
Solved Threads: 1
 
Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
	Dim tspan As TimeSpan = TimeSpan.Parse(txtTime.Text)
	If tspan.Days = 0 AndAlso tspan.Hours = 0 AndAlso tspan.Minutes = 0 AndAlso tspan.Seconds = 0 Then
		Timer1.Stop()
		Return
	End If
        Dim tmpSpan as timeSpan = tspan.Subtract(TimeSpan.FromSeconds(1))
	txtDays.Text = tmpSpan.Days.ToString
        txtHours.Text = tmpSpan.Hours.ToString 'and so on
	End Sub
GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

:-/
MMMM it seems its not working it does show the numbers but doesnt count down

VB 2012
Junior Poster
155 posts since Jul 2010
Reputation Points: 15
Solved Threads: 1
 

Sorry my fault. Here a working snippet:

Public Class Form1

	Private pCurrentCounter as timespan

	Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
		Dim tspan As TimeSpan
		If Not TimeSpan.TryParse(txtTime.Text, tspan) Then
			MsgBox("Please enter the time in format d:h:m:s")
			Return
		End If
		pCurrentCounter=tspan
		Timer1.Interval = 1000
		Timer1.Start()
	End Sub

	Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
		If pCurrentCounter.Days = 0 AndAlso pCurrentCounter.Hours = 0 AndAlso pCurrentCounter.Minutes = 0 AndAlso pCurrentCounter.Seconds = 0 Then
			Timer1.Stop()
			Return
		End If
		pCurrentCounter = pCurrentCounter.Subtract(TimeSpan.FromSeconds(1))
		txtDays.Text=pCurrentCounter.Days.ToString
		txtHours.Text=pCurrentCounter.Hours.ToString
		txtMinutes.Text=pCurrentCounter.Minutes.ToString
		txtSeconds.Text=pCurrentCounter.Seconds.ToString
	End Sub
End Class
GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

Thank you For your Help GEEKBYCHOICE:)
Yu Really should Get a Medal;)

VB 2012
Junior Poster
155 posts since Jul 2010
Reputation Points: 15
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You