|
-
Apr 22nd, 2012, 08:16 PM
#1
Thread Starter
Junior Member
Leading Zero in timer!
I have got my timer ticking but i need to add the leading zero back in but it code i am using only works on the seconds (label3) not on the minutes (label4)!
timer Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label3.Text -= 1
If Label3.Text = "00" Then
Label4.Text -= 1
End If
If Label3.Text = "00" Then
Label3.Text = "60"
End If
If Label3.Text < 10 Then
Label3.Text = "0" & Label3.Text
End If
If Label4.Text < 10 Then
Label4.Text = "0" & Label4.Text
End If
If Label3.Text = "00" And
Label4.Text = "00" Then
Timer1.Stop()
End If
End Sub
-
Apr 22nd, 2012, 09:24 PM
#2
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".
My usual boring signature: Nothing
 
-
Apr 22nd, 2012, 09:31 PM
#3
Thread Starter
Junior Member
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?
-
Apr 22nd, 2012, 09:32 PM
#4
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.
-
Apr 22nd, 2012, 09:41 PM
#5
Thread Starter
Junior Member
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!
-
Apr 22nd, 2012, 09:49 PM
#6
Thread Starter
Junior Member
Re: Leading Zero in timer!
this is what i have so far!
-
Apr 22nd, 2012, 10:11 PM
#7
Thread Starter
Junior Member
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!
-
Apr 22nd, 2012, 10:32 PM
#8
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.
-
Apr 23rd, 2012, 05:42 AM
#9
Thread Starter
Junior Member
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:
-
Apr 23rd, 2012, 05:50 AM
#10
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.
-
Apr 23rd, 2012, 06:12 AM
#11
Thread Starter
Junior Member
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.
-
Apr 23rd, 2012, 06:30 AM
#12
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.
-
Apr 23rd, 2012, 06:38 AM
#13
Thread Starter
Junior Member
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!
-
Apr 23rd, 2012, 08:29 AM
#14
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.
-
Apr 23rd, 2012, 08:35 AM
#15
Thread Starter
Junior Member
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!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|