I was wondering how I would go about fixing my countdown timer so that it shows the time in Hours:Minutes:Seconds?
currently, it only outputs the seconds and I'm not sure where I have gone wrong.
Any help is much appreciated.
Code:
Public Class Form1
Dim TargetDT As DateTime
Dim fields() As String
Dim Days, Hours, minutes, seconds, milliseconds As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
fields = Split(TextBox1.Text, ":")
Hours = fields(0)
minutes = fields(1)
seconds = fields(2)
Dim CountDownFrom As TimeSpan = TimeSpan.FromHours(Hours).FromMinutes(minutes).FromSeconds(seconds)
Timer1.Interval = 1000
TargetDT = DateTime.Now.Add(CountDownFrom)
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
Con1Time.Text = ts.ToString("hh\:mm\:ss")
Else
Con1Time.Text = "0:00:00"
Timer1.Stop()
MessageBox.Show("Done")
End If
End Sub
End Class
Start by assigning the time you want to countdown to a TimeSpan field. When you want to star counting down, call Start on a Timer and a Stopwatch. In the Tick event handler of the Timer, get the Elapsed of the Stopwatch and subtract that from the original time to get the time remaining as a TimeSpan. You can then format and display that. When that time remaining reaches zero, the countdown has expired.
Private countDownTime As TimeSpan
Private countDownWatch As Stopwatch
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
countDownTime = TimeSpan.FromMinutes(30)
countDownWatch = Stopwatch.StartNew()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim timeRemaining = countDownTime - countDownWatch.Elapsed
If timeRemaining < TimeSpan.Zero Then
timeRemaining = TimeSpan.Zero
End If
Label1.Text = timeRemaining.ToString("hh\:mm\:ss")
If timeRemaining = TimeSpan.Zero Then
Timer1.Stop()
MessageBox.Show("Countdown complete")
End If
End Sub
That said, your specific issue is here:
Dim CountDownFrom As TimeSpan = TimeSpan.FromHours(Hours).FromMinutes(minutes).FromSeconds(seconds)
That does not do what you think it does. You're only getting the result of the FromSeconds call. If you want to create a TimeSpan with hours, minutes and seconds then call the appropriate constructor
Dim CountDownFrom As TimeSpan = New TimeSpan(Hours, minutes, seconds)
Of course, those arguments should be Integer values, not Strings. Turn Option Strict On and use the proper data types.
Related
I'm trying to do a project and i'm still learning and this timer thing is something very new to me.
*You are required to build a board game. The game should be played on a 4 x 4 grid in pairs. Each grid cell contains a hidden question which the pair needs to answer.
The players take turns in playing the game. Each player generates two random numbers and place the cell on the grid indicated by the numbers. Once clicked, the question for the clicked grid cell is displayed. For example if the generated number is a 2 and a 3 the player clicks in the 2 x 3 grid cell to view the hidden question. You may use any kind of questions.
A player should only be able to click and view the question hidden beneath the grid cell represented by the random numbers.
At the start of the game, the initial score for each player should have a value of 0. For each question answered, the score increases by 5. You can display the score anywhere within the interface.
Each player should be given a duration of 30 seconds to answer a question. This value should be displayed on the user interface. The value should decrease with time and this decrement should be visible on screen at any point in time. Once the value reaches 0 the other player should be given the chance to answer the question. If both the players fail in answering a question, the question should be marked as unanswered. You can display the time left anywhere within the user interface.
The game should end when all the questions have been attempted by the players. When the game ends, the winner should be announced. The winner should be the one with the highest score.
Once the game is over, the questions and answers along with the players should be displayed.*
This is what its supposed to look like
This is what mine looks like
Now this is working just fine when i click on the 1st button even when they get the answer wrong or they go over time the timers will reset and countdown from 30 but when i click on the 2nd button the timer doesn't work even when i attached a second timer.
here is the code
Public Class Form1
Private TargetDT As DateTime
Private TargetDT1 As DateTime
Public CountDownFrom As TimeSpan = TimeSpan.FromSeconds(33)
Public CountDownFrom1 As TimeSpan = TimeSpan.FromSeconds(33)
Private TargetDTQ2 As DateTime
Private TargetDTQ2a As DateTime
Public CountDownFromQ2 As TimeSpan = TimeSpan.FromSeconds(33)
Public CountDownFromQ2a As TimeSpan = TimeSpan.FromSeconds(33)
Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
TargetDT1 = DateTime.Now.Add(CountDownFrom1)
TargetDTQ2 = DateTime.Now.Add(CountDownFromQ2)
TargetDTQ2a = DateTime.Now.Add(CountDownFromQ2a)
End Sub
Public Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
Dim ts1 As TimeSpan = TargetDT1.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTime1.Text = ts.ToString("ss")
Else
lblTime1.Text = ":00"
Timer1.Stop()
MessageBox.Show("Out of Time!! Now player 2 can answer")
lblTime1.Text = ts1.ToString("ss")
Q1.TextBox1.Hide()
Q1.Button1.Hide()
Q1.TextBox2.Show()
Q1.Button2.Show()
End If
End Sub
Public Sub Timer2_Tick(sender As Object, e As System.EventArgs) Handles Timer2.Tick
Dim ts2 As TimeSpan = TargetDTQ2.Subtract(DateTime.Now)
Dim ts3 As TimeSpan = TargetDTQ2a.Subtract(DateTime.Now)
If ts2.TotalMilliseconds > 0 Then
lblTime1.Text = ts2.ToString("ss")
Else
lblTime1.Text = ":00"
Timer2.Stop()
MessageBox.Show("Out of Time!! Now player 2 can answer")
lblTime1.Text = ts3.ToString("ss")
Q1.TextBox1.Hide()
Q1.Button1.Hide()
Q1.TextBox2.Show()
Q1.Button2.Show()
End If
End Sub
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
Q1.Show()
Timer1.Start()
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
Q2.Show()
Timer2.Start()
End Sub
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
End Sub
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
Application.Restart()
End Sub
End Class
This is my code for the second Button
Public Class Q2
Private TargetDT As DateTime
Public CountDownFrom As TimeSpan = TimeSpan.FromSeconds(36)
Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer3.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
End Sub
Public Sub Timer3_Tick(sender As Object, e As System.EventArgs) Handles Timer3.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
Form1.lblTime2.Text = ts.ToString("ss")
Else
'lblTime1.Text = ":00"
Timer3.Stop()
'MessageBox.Show("Out of Time!! Now player 2 can answer")
'Form1.lblTime1.Text = ts.ToString("ss")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Correctanswer As String
Correctanswer = ("Venus")
Dim result As Integer
result = String.Compare(TextBox1.Text, Correctanswer, True)
Dim result2 = String.Compare(TextBox2.Text, Correctanswer, True)
If result = 0 Then
MessageBox.Show("Bingo!")
Form1.txtP1.Text = Val(Form1.txtP1.Text) + 1
Form1.lblTime1.Text = ":00"
Form1.lblTime1.Hide()
Me.Close()
ElseIf result < 0 Then
MessageBox.Show("Wrong!")
TextBox1.Text = " "
Form1.lblTime1.Text = ":00"
Form1.lblTime1.Hide()
Form1.lblTime2.Show()
Timer3.Start()
TextBox1.Hide()
TextBox2.Show()
Button1.Hide()
Button2.Show()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Correctanswer As String
Correctanswer = ("Venus")
Dim result As Integer
result = String.Compare(TextBox1.Text, Correctanswer, True)
Dim result2 = String.Compare(TextBox2.Text, Correctanswer, True)
If result2 = 0 Then
MessageBox.Show("Bingo!")
Form1.txtP2.Text = Val(Form1.txtP2.Text) + 1
Form1.lblTime2.Text = ":00"
Form1.lblTime2.Hide()
Me.Close()
ElseIf result < 0 Then
MessageBox.Show("Wrong!")
TextBox1.Text = " "
Form1.lblTime1.Text = ":00"
Form1.lblTime1.Hide()
Form1.lblTime2.Show()
Me.Close()
End If
End Sub
End Class
PS. Also if someone can help explain how to change a buttons color from the code and not on click (This is so that the palyers can see what grid they have already clicked on)
So, I'm making a VB.NET software, and want to show minute format as XX minute, e.g: "05 minute left" and not like this: "5 minute left." My code now like this:
min = DateTime.Now.ToString("mm") + NumericUpDown2.Value
I've tried with big "MM" letters but it's just another time format.
Is that a countdown application, and when I'm try to check the time failed.
If Date.Time.Minutes = min Than
Alarm.show
End If
Although this code has not been tested it should give you some ideas of how to proceed. As far as the formatting is concerned - right pew, wrong church. Number.ToString("00")
Private AlarmTime As DateTime
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AlarmTime = DateTime.Now.AddMinutes(NumericUpDown1.Value)
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim TimeLeft As TimeSpan = AlarmTime - DateTime.Now
Dim MinutesLeft As Integer = TimeLeft.Minutes
Label1.Text = MinutesLeft.ToString("00")
If AlarmTime >= DateTime.Now Then
Alarm.Show()
Timer1.Enabled = False
End If
End Sub
The right way to do this is like this:
DateTime.Now.Minute.ToString("00")
This is the scenario, I have three NumericUpDown for the values of second, minute and hour, And if a number is entered in the NumericUpDown i.e. 5, it will be converted to 5000 ms so my Timer can read it as 5 seconds. I'm trying to make a scheduler for every interval and this is my code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label13.Text = TimeSpan.FromSeconds(Form3.NumericUpDown1.Text).TotalMilliseconds
Label14.Text = TimeSpan.FromMinutes(Form3.NumericUpDown2.Text).TotalSeconds
Label15.Text = TimeSpan.FromHours(Form3.NumericUpDown3.Text).TotalMinutes
Dim times() As String = {Label13.Text, Label14.Text, Label15.Text}
For Each time In times
Dim interval As TimeSpan = TimeSpan.Parse(time)
Timer1.Interval = interval
MsgBox("hey")
Next
End Sub
And it has an error at Timer1.Interval = interval that says Timespan cannot be converted to Integer, and i can't think of any way to fix this, can you help me out? TIA~!
Try This
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
Timer1.Interval = New TimeSpan(0, 0, CInt(nudMinutes.Value), CInt(nudSeconds.Value), CInt(nudMilliseconds.Value)).TotalMilliseconds
Timer1.Start()
End Sub
Here are some ideas to get you past your current error.
Dim tsTest As TimeSpan = TimeSpan.FromSeconds(NumericUpDownTest.Value)
Label1.Text = tsTest.TotalSeconds.ToString("n0")
Timer1.Interval = CInt(tsTest.TotalMilliseconds)
Start with just doing seconds and when that code works see if you can add the others.
In the following code I have a timer that counts down from 5 mins. I am trying to have a visual count down timer in a lbl in mm:ss but the example I used doesn't work. It counts down but doesn't update the lbl until it hits 00:00.
The asker of the following question (were I got the code) said it works perfectly but for me it doesn't at all.
The Example I used
My code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
picLogo.SizeMode = PictureBoxSizeMode.StretchImage
'Timer until update
tmrUpdate.Interval = 300000 '5 minutes
TargetDT = DateTime.Now.Add(CountDownFrom)
tmrUpdate.Enabled = True
End Sub
Private Sub tmrUpdate_Tick(sender As Object, e As EventArgs) Handles tmrUpdate.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTimer.Text = ts.ToString("mm\:ss")
Else
lblTimer.Text = "00:00"
tmrUpdate.Stop()
End If
End Sub
Answer:
Using a Async Sub I had the count down timer running while other stuff was going on in the back ground. This way the app could still be used during the Sub Wait() and this code also displayed the count down timer.
Used one timer on a 1 sec interval.
Private Async Sub DoStuff()
'Doing stuff
timeUpDate = 599
tmrUpdate.Start()
Application.DoEvents()
Await Task.Run(Sub()
Wait()
End Sub)
Loop
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles tmrUpdate.Tick
Dim hms = TimeSpan.FromSeconds(timeUpDate)
Dim m = hms.Minutes.ToString
Dim s = hms.Seconds.ToString
If timeUpDate > 0 Then
timeUpDate -= 1
lblTimer.Text = (m & ":" & s)
Else
tmrUpdate.Stop()
lblTimer.Text = "text"
End If
End Sub
Private Sub Wait()
Threading.Thread.Sleep(600000)
End Sub
how To add minutes to an existing countdown timer like while the timer is counting down I can add 10 mins with a push of a button so the time pluses like if the displayed time is 9.59 with the press of a button the time should display 19.59 and continue counting down
Public Class TIMER
Private alarmtime As Date
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If alarmtime < Date.Now Then
Me.Timer1.Stop()
MessageBox.Show("Time up")
Else
Dim remainingtime As TimeSpan = Me.alarmtime.Subtract(Date.Now)
Me.Label1.Text = String.Format("{0}:{1:d2}:{2:d2}", _
remainingtime.Hours, _
remainingtime.Minutes, _
remainingtime.Seconds)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
Me.alarmtime = Date.Now.AddHours(TextBox1.Text)
Me.Timer1.Start()
ElseIf RadioButton2.Checked = True Then
Me.alarmtime = Date.Now.AddMinutes(TextBox1.Text)
Me.Timer1.Start()
ElseIf RadioButton3.Checked = True Then
Me.alarmtime = Date.Now.AddSeconds(TextBox1.Text)
Me.Timer1.Start()
End If
End Sub
Private Sub GameTIMER_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
its just a basic countdown timer it works fine all I need for it to do is add minutes to it while its is counting down because when the timer is up I have to click the text box type in the number then press ok then the timer starts so instead off writhing in the textbox I just want to press a button and the countdown timer automatically adds lets say 10 minutes to current time being displayed while its counting down
If you're talking about a normal Timer, this should be your answer:
Me.Timer1.Interval += (60000 * 10)
EDIT:
Try stopping it first:
Me.Timer1.Stop()
Me.Timer1.Interval += (60000 * 10)
Me.Timer1.Start()