Results 1 to 15 of 15

Thread: Leading Zero in timer!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Location
    Edinburgh - Scotland
    Posts
    19

    Exclamation 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:
    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2.         Label3.Text -= 1
    3.         If Label3.Text = "00" Then
    4.             Label4.Text -= 1
    5.         End If
    6.         If Label3.Text = "00" Then
    7.             Label3.Text = "60"
    8.         End If
    9.         If Label3.Text < 10 Then
    10.             Label3.Text = "0" & Label3.Text
    11.         End If
    12.         If Label4.Text < 10 Then
    13.             Label4.Text = "0" & Label4.Text
    14.         End If
    15.         If Label3.Text = "00" And
    16.             Label4.Text = "00" Then
    17.             Timer1.Stop()
    18.         End If
    19.     End Sub

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Location
    Edinburgh - Scotland
    Posts
    19

    Wink 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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private countDownTime As TimeSpan = TimeSpan.FromMinutes(2)
    2.  
    3. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    4.     countDownTime = countDownTime - TimeSpan.FromSeconds(1)
    5.     minutesLabel.Text = countDownTime.Minutes.ToString("00")
    6.     secondsLabel.Text = countDownTime.Seconds.ToString("00")
    7. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Location
    Edinburgh - Scotland
    Posts
    19

    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!

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Location
    Edinburgh - Scotland
    Posts
    19

    Red face Re: Leading Zero in timer!

    this is what i have so far!
    form1 Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    4.         Me.Close()
    5.     End Sub
    6.  
    7.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, MyBase.Load
    8.         Form2.Show()
    9.     End Sub
    10.  
    11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         Form3.ShowDialog()
    13.     End Sub
    14.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    15.         Timer1.Enabled = False
    16.  
    17.     End Sub
    18.  
    19.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    20.         If Label3.Text > 0 Then
    21.             Label3.Text = Label3.Text - 1
    22.         ElseIf Label3.Text <= 0 Then
    23.             Label4.Text = Label4.Text - 1
    24.             Label3.Text = 59
    25.         End If
    26.         If Label3.Text <= 9 Then
    27.             Label3.Text = "0" & Label3.Text
    28.         End If
    29.         If Label3.Text = "00" And
    30.             Label4.Text = "00" Then
    31.             Timer1.Stop()
    32.         End If
    33.     End Sub
    34.  
    35.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, MyBase.Load
    36.         Timer1.Enabled = True
    37.         If Form2.ComboBox1.Text = "5mins" And
    38.             Label1.Text = "ROUND 1" Then
    39.             Label4.Text = "04"
    40.             Label3.Text = "59"
    41.         ElseIf Form2.ComboBox1.Text = "10mins" And
    42.             Label1.Text = "ROUND 1" Then
    43.             Label4.Text = "09"
    44.             Label3.Text = "59"
    45.         ElseIf Form2.ComboBox1.Text = "15mins" And
    46.             Label1.Text = "ROUND 1" Then
    47.             Label4.Text = "14"
    48.             Label3.Text = "59"
    49.         ElseIf Form2.ComboBox1.Text = "20mins" And
    50.             Label1.Text = "ROUND 1" Then
    51.             Label4.Text = "19"
    52.             Label3.Text = "59"
    53.         ElseIf Form2.ComboBox1.Text = "25mins" And
    54.             Label1.Text = "ROUND 1" Then
    55.             Label4.Text = "24"
    56.             Label3.Text = "59"
    57.         ElseIf Form2.ComboBox1.Text = "30mins" And
    58.             Label1.Text = "ROUND 1" Then
    59.             Label4.Text = "29"
    60.             Label3.Text = "59"
    61.         End If
    62.     End Sub
    63. End Class

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Location
    Edinburgh - Scotland
    Posts
    19

    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!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Location
    Edinburgh - Scotland
    Posts
    19

    Re: Leading Zero in timer!

    I am trying your code but with no success I maybe doing something wrong!

    im trying Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, MyBase.Load
    2.         Timer1.Enabled = True
    3.     End Sub
    4.  
    5.     Private countDownTime As TimeSpan = TimeSpan.FromMinutes(5)
    6.  
    7.  
    8.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, MyBase.Load
    9.         If Label1.Text = "ROUND 1" Then
    10.             Dim minutes As Integer = (Form2.ComboBox1.SelectedIndex + 1) * 5
    11.         End If
    12.         countDownTime = countDownTime - TimeSpan.FromSeconds(1)
    13.         minutesLabel.Text = countDownTime.Minutes.ToString("00")
    14.         secondsLabel.Text = countDownTime.Seconds.ToString("00")
    15.         If secondsLabel.Text = "00" And
    16.             minutesLabel.Text = "00" Then
    17.             Timer1.Stop()
    18.         End If
    19.     End Sub
    20.  
    21. End Class
    22. :confused:

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Location
    Edinburgh - Scotland
    Posts
    19

    Re: Leading Zero in timer!

    ok!
    vb Code:
    1. Private countDownTime As TimeSpan = TimeSpan.FromMinutes(5)
    2.  
    3.  
    4.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick, MyBase.Load
    5.  
    6.         Dim minutes As Integer = (mycombobox.SelectedIndex + 1) * 5
    7.  
    8.         countDownTime = countDownTime - TimeSpan.FromSeconds(1)
    9.         minutesLabel.Text = countDownTime.Minutes.ToString("00")
    10.         secondsLabel.Text = countDownTime.Seconds.ToString("00")
    11.         If secondsLabel.Text = "00" And
    12.             minutesLabel.Text = "00" Then
    13.             Timer1.Stop()
    14.         End If
    15.     End Sub

    still no luck it says:
    Error 1 'mycombobox' is not declared. It may be inaccessible due to its protection level.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Location
    Edinburgh - Scotland
    Posts
    19

    Re: Leading Zero in timer!

    so how do i go about stopping this? im so confused! i tried deleting the following!
    this Code:
    1. Private countDownTime As TimeSpan = TimeSpan.FromMinutes(5)

    but that mean this line didnt work

    this Code:
    1. countDownTime = countDownTime - TimeSpan.FromSeconds(1)

    as for the other line
    this Code:
    1. 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!

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Location
    Edinburgh - Scotland
    Posts
    19

    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
  •  



Click Here to Expand Forum to Full Width