Re: Leading Zero in timer!
Take a look at the PadLeft method of the string, that should take care of what you are trying to do without requiring all those If statements. Basically, you take the string and padd it to the proper length with "0".
Re: Leading Zero in timer!
I have just checked out the pad left string method but I'm not 100% on how to apply it to this! Could you show me some example code?
Re: Leading Zero in timer!
You're going about this is very much the wrong way. You should not be subtracting anything from the Label Text. You should be storing your values in numeric variables. Any calculations take place using those variables. You then convert the numbers to Strings to display and that's when you specify that a leading zero should be used.
In fact, you should not even be using a numeric variable. You should be using a TimeSpan, which will calculate the minutes and seconds for you, e.g.
vb.net Code:
Private countDownTime As TimeSpan = TimeSpan.FromMinutes(2)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
countDownTime = countDownTime - TimeSpan.FromSeconds(1)
minutesLabel.Text = countDownTime.Minutes.ToString("00")
secondsLabel.Text = countDownTime.Seconds.ToString("00")
Next
That said, you really shouldn't be subtracting one second each Tick because you don't know for sure that one second has passed. You should be using either DateTime subtraction to calculate time or using a Stopwatch.
Re: Leading Zero in timer!
Oh I see! I am very new to this as you can probably tell! My situation is that in form2 i have a set of comboboxes with options such as : 5mins, 10mins ect so each time the timer runs down I need it too take the combobox selection and make that the time my countdown starts at each time! By applying to the method you have told I just don't know the best to di this!
Re: Leading Zero in timer!
this is what i have so far!
Re: Leading Zero in timer!
i have applied your code to my my code and it works really well only i need the timer to start at the selected option on the combobox in another form!
Re: Leading Zero in timer!
Notice how, in my example, I initialise the TimeSpan by calling TimeSpan.FromMinutes and passing it a number of minutes? That's what you should do, so you need to get a numeric value from the ComboBox. There are various ways to do that but, because you're using regular intervals of 5 minutes, you get to do it the easy way. You can simply get the SelectedIndex of the ComboBox, add 1 and then multiply by 5:
vb.net Code:
Dim minutes As Integer = (myComboBox.SelectedIndex + 1) * 5
The index of the first item is zero, so add 1 and multiply by 5 gives 5. The index of the second item is 1, so add 1 and multiply by 5 gives 10. Etc.
Re: Leading Zero in timer!
I am trying your code but with no success I maybe doing something wrong!
im trying Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, MyBase.Load
Timer1.Enabled = True
End Sub
Private countDownTime As TimeSpan = TimeSpan.FromMinutes(5)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, MyBase.Load
If Label1.Text = "ROUND 1" Then
Dim minutes As Integer = (Form2.ComboBox1.SelectedIndex + 1) * 5
End If
countDownTime = countDownTime - TimeSpan.FromSeconds(1)
minutesLabel.Text = countDownTime.Minutes.ToString("00")
secondsLabel.Text = countDownTime.Seconds.ToString("00")
If secondsLabel.Text = "00" And
minutesLabel.Text = "00" Then
Timer1.Stop()
End If
End Sub
End Class
:confused:
Re: Leading Zero in timer!
Go back and read post #8 again and then look at your code and ask yourself whether you're doing what I said to do.
Re: Leading Zero in timer!
ok!
vb Code:
Private countDownTime As TimeSpan = TimeSpan.FromMinutes(5)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, MyBase.Load
Dim minutes As Integer = (mycombobox.SelectedIndex + 1) * 5
countDownTime = countDownTime - TimeSpan.FromSeconds(1)
minutesLabel.Text = countDownTime.Minutes.ToString("00")
secondsLabel.Text = countDownTime.Seconds.ToString("00")
If secondsLabel.Text = "00" And
minutesLabel.Text = "00" Then
Timer1.Stop()
End If
End Sub
still no luck it says:
Error 1 'mycombobox' is not declared. It may be inaccessible due to its protection level.
Re: Leading Zero in timer!
"myComboBox" is just an example. You're supposed to use YOUR ComboBox.
What I said to do was to get a number from the ComboBox, turn that into a number of minutes and then use that to initialise the TimeSpan for count down. You're initialising the time with 5 minutes no matter what and you're getting the number of minutes from the ComboBox every time the Timer Ticks and never using the value.
Re: Leading Zero in timer!
so how do i go about stopping this? im so confused! i tried deleting the following!
this Code:
Private countDownTime As TimeSpan = TimeSpan.FromMinutes(5)
but that mean this line didnt work
this Code:
countDownTime = countDownTime - TimeSpan.FromSeconds(1)
as for the other line
this Code:
Dim minutes As Integer = (mycombobox.SelectedIndex + 1) * 5
i need this to happen to 1 of 30 comboboxes depending on how many times the timer hase restarted eg what round we are on!
Re: Leading Zero in timer!
You're confused because you're trying to write code without knowing what that code is actually supposed to do. Have you actually written down the steps the code needs to perform? I'll wager not. If you haven't got a clear idea of what the code is supposed to do then what hope have you got of winding up with code that does it?
What's the whole point here? You're supposed to be counting down based on the selection the user makes in the ComboBox, right? So what use is it using a constant 5 minutes to create the count down time? What value should you actually be using to create the count down time? The value from the ComboBox.
Also, why are you getting the value from the ComboBox inside the Tick event handler? The value from the ComboBox is supposed to be the amount of time the count down goes for, so you only need to know that once, at the start. Where do you want to start the count down? That's where you get the value and create the count down time.
Again, don;t even think of write any code until you have a clear idea of EXACTLY what that code has to do. Sticking random code in random places is never going to work. If you don't know exactly why you're doing something, don't do it. If you need to pick up a pen and paper and write down the steps in order to get that clear idea, that's exactly what you should be doing. You should be developing an algorithm first, then converting that algorithm to pseudo-code, then converting that to real code.
Re: Leading Zero in timer!
Jesus .. deep! well im learning and im learning from my mistakes the problem is solved and turns out ten times easier! someone gave me a pointer ( someone who understood the fact i was learning!) but thanks for your time! :wave: