Results 1 to 22 of 22

Thread: Help me with this code.

  1. #1

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Question Help me with this code.

    Hello!
    I am using Visual Studio Express 2013 or Visual Basic 12.0.
    I am programming a timer that partly works.
    The problem is that after I set the timer and the timer is done, I cannot set it again.
    I have 5 labels (2 for : (not in code) and 3 for the hours (hours_label), minutes (minutes_label) and seconds (seconds_label)), 3 buttons (Set (ok_button), Restart (Restart), Start (Start_Pause)), a timer (Timer3) and 3 textboxes (to input the amount of time(hours_textbox, minutes_textbox, seconds_textbox)).

    Name:  Timer.JPG
Views: 1340
Size:  12.6 KB

    Here is the code:

    Code:
    Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
            hours_label.Text = "00"
            minutes_label.Text = "00"
            seconds_label.Text = "00"
            Timer3.Enabled = False
        End Sub
        Private Sub Start_pause_Click(sender As Object, e As EventArgs) Handles Start_pause.Click
            If Start_pause.Text = "Start" Then
                Timer3.Enabled = True
                ok_button.Enabled = False
                Start_pause.Text = "Pause"
            ElseIf Start_pause.Text = "Pause" Then
                Timer3.Enabled = False
                Restart.Enabled = True
                ok_button.Enabled = True
                Start_pause.Text = "Start"
            End If
        End Sub
        Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
            Timer3.Interval = 1000
            If hours_label.Text = "" Then
                hours_label.Text = "0"
            End If
            If minutes_label.Text = "" Then
                minutes_label.Text = "0"
            End If
            If seconds_label.Text = "" Then
                seconds_label.Text = "0"
            End If
            If hours_label.Text = "00" Then
                hours_label.Text = "0"
            End If
            If minutes_label.Text = "00" Then
                minutes_label.Text = "0"
            End If
            If seconds_label.Text = "00" Then
                seconds_label.Text = "0"
            End If
            If seconds_label.Text > "0" Then
                seconds_label.Text = seconds_label.Text - 1
            End If
            If minutes_label.Text > "0" Then
                If seconds_label.Text = "0" Then
                    minutes_label.Text = minutes_label.Text - 1
                    seconds_label.Text = "59"
                End If
            End If
            If hours_label.Text > "0" Then
                If minutes_label.Text = "0" Then
                    If seconds_label.Text = "0" Then
                        hours_label.Text = hours_label.Text - 1
                        minutes_label.Text = "59"
                        seconds_label.Text = "59"
                    End If
                End If
            End If
            If seconds_label.Text = "0" Then
                If minutes_label.Text = "0" Then
                    If hours_label.Text = "0" Then
                        Timer3.Enabled = False
                        Beep()
                        MsgBox("Time is up")
                        Start_pause.Text = "Start"
                        Restart.Enabled = False
                    End If
                End If
            End If
        End Sub
        Private Sub ok_button_Click(sender As Object, e As EventArgs) Handles ok_button.Click
            If seconds_textbox.Text = "00" Then
                If minutes_textbox.Text = "00" Then
                    If hours_textbox.Text = "00" Then
                        Start_pause.Enabled = False
                        MsgBox("Please enter a value higher then 0")
                    End If
                End If
            End If
            If seconds_textbox.Text = "0" Then
                If minutes_textbox.Text = "0" Then
                    If hours_textbox.Text = "0" Then
                        Start_pause.Enabled = False
                        MsgBox("Please enter a value higher then 0")
                    End If
                End If
            End If
            If seconds_textbox.Text = "" Then
                minutes_textbox.Text = ""
                hours_textbox.Text = ""
                Start_pause.Enabled = False
                MsgBox("Please enter a value")
            End If
            ok_button.Enabled = True
            hours_label.Text = hours_textbox.Text
            minutes_label.Text = minutes_textbox.Text
            seconds_label.Text = seconds_textbox.Text
            If hours_label.Text > "0" Then
                Start_pause.Enabled = True
            End If
            If minutes_label.Text > "0" Then
                Start_pause.Enabled = True
            End If
            If seconds_label.Text > "0" Then
                Start_pause.Enabled = True
            End If
        End Sub
    Last edited by dday9; Aug 15th, 2014 at 04:29 PM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Help me with this code.

    Hi Cow124,

    I moved your thread to the VB.Net forums. I also added code tags to your code. Code tags keep your code formatted and helps with readability. Whenever you're in Advanced Reply or Quick Reply it is a button you can hit next next to the Quote button. It's icon is the # sign. Otherwise you can type it out manually, to do that type:
    [code]'Hello world[/code]

    That translates to:
    Code:
    'Hello world
    Anyways, welcome to VBForums and good luck with your problem!
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Help me with this code.

    You're going to have several problems with that code. I'd suggest that you switch to using NumericUpDown controls rather than textboxes. Textboxes accept strings, so a person could enter pretty nearly anything, including things that aren't numbers.

    Another point you will have an issue with is this (and any lines like it):

    If minutes_label.Text > "0" Then

    I'm not sure what you think that will do, but the > operator IS defined for strings, so what is actually going to happen is that you will get an alphabetic comparison. That should mean that any character is greater than "0", as is "00", "01", "000000", and so forth.

    This line probably won't cause you any trouble:

    seconds_label.Text = seconds_label.Text - 1

    but it very well might if you had used + rather than -. What is happening is that the compiler is implicitly converting the label text to a number, then subtracting 1, then converting the result back to a string and putting it in the label. Had you used +, the compiler might also convert the 1 to a string (I would argue that it SHOULD do this), then concatenate the 1 onto the end of whatever string is in the label text. Therefore, if the label text was "0", you would get "01" then "011", and so forth.

    Essentially, you are using strings where you should be using numbers, and since you have Option Strict OFF, the compiler is letting you get away with that, even though it isn't always going to do what you want it to do. What you should do is turn Option Strict ON and fix all the errors that result, because they are real errors. Use numbers where you are doing math, use strings when you are displaying things, and properly convert from one to the other. In that last example, it's easy:

    seconds_label.Text = (CInt(seconds_label.Text) - 1).ToString

    in the previous example, it isn't quite as easy, because you are using a textbox, so the user could enter anything at all. Therefore, you would have to use Integer.TryParse, and deal with the case where it returns False, which would mean that the text can't be converted. Using a NUD would fix that, since NumericUpDown.Value is a decimal, and you can set the minimum and maximum values such that the code where you put a "0" in the textbox when it is empty would not be needed.

    The first part probably addresses your actual question, too. Since you are comparing strings, it is quite possible that your comparisons are not doing what you think they are doing.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Hello again!
    The same problem is still there: after I use the timer once, it cannot reset or set.
    ShaggyHiker's first point may have answered this question (Not using <), but I'm not sure how to replace it with something numerical.
    Code:
        Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
            hours_label.Text = "00"
            minutes_label.Text = "00"
            seconds_label.Text = "00"
            Timer3.Enabled = False
        End Sub
        Private Sub Start_pause_Click(sender As Object, e As EventArgs) Handles Start_pause.Click
            If Start_pause.Text = "Start" Then
                Timer3.Enabled = True
                ok_button.Enabled = False
                Start_pause.Text = "Pause"
            ElseIf Start_pause.Text = "Pause" Then
                Timer3.Enabled = False
                Restart.Enabled = True
                ok_button.Enabled = True
                Start_pause.Text = "Start"
            End If
        End Sub
        Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
            Timer3.Interval = 1000
            If hours_label.Text = "" Then
                hours_label.Text = "0"
            End If
            If minutes_label.Text = "" Then
                minutes_label.Text = "0"
            End If
            If seconds_label.Text = "" Then
                seconds_label.Text = "0"
            End If
            If hours_label.Text = "00" Then
                hours_label.Text = "0"
            End If
            If minutes_label.Text = "00" Then
                minutes_label.Text = "0"
            End If
            If seconds_label.Text = "00" Then
                seconds_label.Text = "0"
            End If
            If seconds_label.Text > "0" Then
                seconds_label.Text = CStr(CDbl(seconds_label.Text) - 1)
            End If
            If minutes_label.Text > "0" Then
                If seconds_label.Text = "0" Then
                    minutes_label.Text = CStr(CDbl(minutes_label.Text) - 1)
                    seconds_label.Text = "59"
                End If
            End If
            If hours_label.Text > "0" Then
                If minutes_label.Text = "0" Then
                    If seconds_label.Text = "0" Then
                        hours_label.Text = CStr(CDbl(hours_label.Text) - 1)
                        minutes_label.Text = "59"
                        seconds_label.Text = "59"
                    End If
                End If
            End If
            If seconds_label.Text = "0" Then
                If minutes_label.Text = "0" Then
                    If hours_label.Text = "0" Then
                        Timer3.Enabled = False
                        Beep()
                        MsgBox("Time is up")
                        Start_pause.Text = "Start"
                        Restart.Enabled = False
                    End If
                End If
            End If
        End Sub
        Private Sub ok_button_Click(sender As Object, e As EventArgs) Handles ok_button.Click
            If NumericUpDown1.Text = "0" Then
                If NumericUpDown2.Text = "0" Then
                    If NumericUpDown3.Text = "0" Then
                        Start_pause.Enabled = False
                        MsgBox("Please enter a value higher then 0")
                    End If
                End If
            End If
            ok_button.Enabled = True
            hours_label.Text = NumericUpDown1.Text()
            minutes_label.Text = NumericUpDown2.Text
            seconds_label.Text = NumericUpDown3.Text
            If hours_label.Text > "0" Then
                Start_pause.Enabled = True
            End If
            If minutes_label.Text > "0" Then
                Start_pause.Enabled = True
            End If
            If seconds_label.Text > "0" Then
                Start_pause.Enabled = True
            End If
        End Sub

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Help me with this code.

    You should not be comparing the Text of a Label to anything. Labels are for display and that is all they should be used for. If you want to work with numbers then work with numbers. If you want Integers then declare some Integer variables and store your numbers there. You can then use those numbers in all your comparisons and calculations. If you want to display one of those numbers in a Label then you call ToString on it and assign the result to the Text property of the Label. That should be the sum total of the Label's involvement.

    I would suggest that you should actually be using a single TimeSpan for the time period. It has Hours, Minutes and Seconds properties that you can get the individual time components from. It also has methods for calculations, e.g. AddSeconds(10) to add 10 seconds or AddMinutes(-1) to subtract 1 minute.

  6. #6

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    How would you use the timespan in my code (I am not familiar with it)?
    I took your advice and instead of using the labels, I used NUDs to compare.
    Nothing has changed, but we are getting there
    Code:
     Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
            hours_label.Text = "00"
            minutes_label.Text = "00"
            seconds_label.Text = "00"
            Timer3.Enabled = False
        End Sub
        Private Sub Start_pause_Click(sender As Object, e As EventArgs) Handles Start_pause.Click
            If Start_pause.Text = "Start" Then
                Timer3.Enabled = True
                ok_button.Enabled = False
                Start_pause.Text = "Pause"
            ElseIf Start_pause.Text = "Pause" Then
                Timer3.Enabled = False
                Restart.Enabled = True
                ok_button.Enabled = True
                Start_pause.Text = "Start"
            End If
        End Sub
        Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
            Timer3.Interval = 1000
            If NumericUpDown1.Text = "" Then
                NumericUpDown1.Text = "0"
            End If
            If NumericUpDown2.Text = "" Then
                NumericUpDown2.Text = "0"
            End If
            If NumericUpDown3.Text = "" Then
                NumericUpDown3.Text = "0"
            End If
            If NumericUpDown1.Text = "00" Then
                NumericUpDown1.Text = "0"
            End If
            If NumericUpDown2.Text = "00" Then
                NumericUpDown2.Text = "0"
            End If
            If NumericUpDown3.Text = "00" Then
                NumericUpDown3.Text = "0"
            End If
            If NumericUpDown3.Text > "0" Then
                NumericUpDown3.Text = CStr(CDbl(NumericUpDown3.Text) - 1)
            End If
            If NumericUpDown2.Text > "0" Then
                If NumericUpDown3.Text = "0" Then
                    NumericUpDown2.Text = CStr(CDbl(NumericUpDown2.Text) - 1)
                    NumericUpDown3.Text = "59"
                End If
            End If
            If NumericUpDown1.Text > "0" Then
                If NumericUpDown2.Text = "0" Then
                    If NumericUpDown3.Text = "0" Then
                        NumericUpDown1.Text = CStr(CDbl(NumericUpDown1.Text) - 1)
                        NumericUpDown2.Text = "59"
                        NumericUpDown3.Text = "59"
                    End If
                End If
            End If
            If NumericUpDown3.Text = "0" Then
                If NumericUpDown2.Text = "0" Then
                    If NumericUpDown1.Text = "0" Then
                        Timer3.Enabled = False
                        Beep()
                        MsgBox("Time is up")
                        Start_pause.Text = "Start"
                        Restart.Enabled = False
                    End If
                End If
            End If
        End Sub
        Private Sub ok_button_Click(sender As Object, e As EventArgs) Handles ok_button.Click
            If NumericUpDown1.Text = "0" Then
                If NumericUpDown2.Text = "0" Then
                    If NumericUpDown3.Text = "0" Then
                        Start_pause.Enabled = False
                        MsgBox("Please enter a value higher then 0")
                    End If
                End If
            End If
            ok_button.Enabled = True
            hours_label.Text = NumericUpDown1.Text()
            minutes_label.Text = NumericUpDown2.Text
            seconds_label.Text = NumericUpDown3.Text
            If NumericUpDown1.Text > "0" Then
                Start_pause.Enabled = True
            End If
            If NumericUpDown2.Text > "0" Then
                Start_pause.Enabled = True
            End If
            If NumericUpDown3.Text > "0" Then
                Start_pause.Enabled = True
            End If
        End Sub

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Help me with this code.

    Don't set the Text property of a NumericUpDown, rather set it's Value property. And rather than checking it the Value matches a string literal, check if it matches an Integer. Also whenever you set the value, use the -= operator:
    Code:
    NumericUpDown.Value -= 1
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    There are parts of my code like this, where there is no value, how would I do that:
    Code:
    If NumericUpDown1.Text = ""

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Help me with this code.

    Set the value equal to 0.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  10. #10

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Now my code is kind of messed up lots of expecting end of statement errors and and option strict is not allowing string to double.
    Code:
            Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
            hours_label.Text = "00"
            minutes_label.Text = "00"
            seconds_label.Text = "00"
            Timer3.Enabled = False
        End Sub
        Private Sub Start_pause_Click(sender As Object, e As EventArgs) Handles Start_pause.Click
            If Start_pause.Text = "Start" Then
                Timer3.Enabled = True
                ok_button.Enabled = False
                Start_pause.Text = "Pause"
            ElseIf Start_pause.Text = "Pause" Then
                Timer3.Enabled = False
                Restart.Enabled = True
                ok_button.Enabled = True
                Start_pause.Text = "Start"
            End If
        End Sub
        Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
            Timer3.Interval = 1000
            If CBool(NumericUpDown1.Value) -=  " " Then
                NumericUpDown1.Value -= 0
            End If
            If NumericUpDown2.Value -= "" Then
                NumericUpDown2.Value -= 0
            End If
            If NumericUpDown3.Value -= "" Then
                NumericUpDown3.Value -= 0
            End If
            If NumericUpDown1.Value -= 00 Then
                NumericUpDown1.Value -= 0
            End If
            If NumericUpDown2.Value -= 00 Then
                NumericUpDown2.Value -= 0
            End If
            If NumericUpDown3.Value -= 00 Then
                NumericUpDown3.Value -= 0
            End If
            If NumericUpDown3.Value > 0 Then
                NumericUpDown3.Value -= CStr(CDbl(NumericUpDown3.Value) - 1)
            End If
            If NumericUpDown2.Value > 0 Then
                If CBool(NumericUpDown3.Value) -= 0 Then
                    NumericUpDown2.Value -= CStr(CDbl(NumericUpDown2.Value) - 1)
                    NumericUpDown3.Value -= 59
                End If
            End If
            If NumericUpDown1.Value > 0 Then
                If CBool(NumericUpDown2.Value) -= 0 Then
                    If CBool(NumericUpDown3.Value) -= 0 Then
                        NumericUpDown1.Value -= CStr(CDbl(NumericUpDown1.Value) - 1)
                        NumericUpDown2.Value -= 59
                        NumericUpDown3.Value -= 59
                    End If
                End If
            End If
            If CBool(NumericUpDown3.Value) -= 0 Then
                If CBool(NumericUpDown2.Value) -= 0 Then
                    If NumericUpDown1.Value -= 0 Then
                        CShort(Timer3.Enabled) -= False
                        Beep()
                        MsgBox("Time is up")
                        Start_pause.Text = "Start"
                        Restart.Enabled = False
                    End If
                End If
            End If
        End Sub
        Private Sub ok_button_Click(sender As Object, e As EventArgs) Handles ok_button.Click
            If CBool(NumericUpDown1.Value) -= 0 Then
                If CBool(NumericUpDown2.Value) -= 0 Then
                    If CBool(NumericUpDown3.Value) -= 0 Then
                        Start_pause.Enabled = False
                        MsgBox("Please enter a value higher then 0")
                    End If
                End If
            End If
            ok_button.Enabled = True
            hours_label.Text = CStr(NumericUpDown1.Value)
            minutes_label.Text = CStr(NumericUpDown2.Value)
            seconds_label.Text = CStr(NumericUpDown3.Value)
            If NumericUpDown1.Value > 0 Then
                Start_pause.Enabled = True
            End If
            If NumericUpDown2.Value > 0 Then
                Start_pause.Enabled = True
            End If
            If NumericUpDown3.Value > 0 Then
                Start_pause.Enabled = True
            End If
        End Sub

  11. #11

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Here is the code
    Code:
     Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
            hours_label.Text = "00"
            minutes_label.Text = "00"
            seconds_label.Text = "00"
            Timer3.Enabled = False
        End Sub
        Private Sub Start_pause_Click(sender As Object, e As EventArgs) Handles Start_pause.Click
            If Start_pause.Text = "Start" Then
                Timer3.Enabled = True
                ok_button.Enabled = False
                Start_pause.Text = "Pause"
            ElseIf Start_pause.Text = "Pause" Then
                Timer3.Enabled = False
                Restart.Enabled = True
                ok_button.Enabled = True
                Start_pause.Text = "Start"
            End If
        End Sub
        Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
            Timer3.Interval = 1000
            If CBool(NumericUpDown1.Value) -= 0 Then
                NumericUpDown1.Value -= 0
            End If
            If NumericUpDown2.Value -= 0 Then
                NumericUpDown2.Value -= 0
            End If
            If NumericUpDown3.Value -= 0 Then
                NumericUpDown3.Value -= 0
            End If
            If NumericUpDown1.Value -= 00 Then
                NumericUpDown1.Value -= 0
            End If
            If NumericUpDown2.Value -= 00 Then
                NumericUpDown2.Value -= 0
            End If
            If NumericUpDown3.Value -= 00 Then
                NumericUpDown3.Value -= 0
            End If
            If NumericUpDown3.Value > 0 Then
                NumericUpDown3.Value -= CStr(CDbl(NumericUpDown3.Value) - 1)
            End If
            If NumericUpDown2.Value > 0 Then
                If CBool(NumericUpDown3.Value) -= 0 Then
                    NumericUpDown2.Value -= CStr(CDbl(NumericUpDown2.Value) - 1)
                    NumericUpDown3.Value -= 59
                End If
            End If
            If NumericUpDown1.Value > 0 Then
                If CBool(NumericUpDown2.Value) -= 0 Then
                    If CBool(NumericUpDown3.Value) -= 0 Then
                        NumericUpDown1.Value -= CStr(CDbl(NumericUpDown1.Value) - 1)
                        NumericUpDown2.Value -= 59
                        NumericUpDown3.Value -= 59
                    End If
                End If
            End If
            If CBool(NumericUpDown3.Value) -= 0 Then
                If CBool(NumericUpDown2.Value) -= 0 Then
                    If NumericUpDown1.Value -= 0 Then
                        CShort(Timer3.Enabled) -= False
                        Beep()
                        MsgBox("Time is up")
                        Start_pause.Text = "Start"
                        Restart.Enabled = False
                    End If
                End If
            End If
        End Sub
        Private Sub ok_button_Click(sender As Object, e As EventArgs) Handles ok_button.Click
            If CBool(NumericUpDown1.Value) -= 0 Then
                If CBool(NumericUpDown2.Value) -= 0 Then
                    If CBool(NumericUpDown3.Value) -= 0 Then
                        Start_pause.Enabled = False
                        MsgBox("Please enter a value higher then 0")
                    End If
                End If
            End If
            ok_button.Enabled = True
            hours_label.Text = CStr(NumericUpDown1.Value)
            minutes_label.Text = CStr(NumericUpDown2.Value)
            seconds_label.Text = CStr(NumericUpDown3.Value)
            If NumericUpDown1.Value > 0 Then
                Start_pause.Enabled = True
            End If
            If NumericUpDown2.Value > 0 Then
                Start_pause.Enabled = True
            End If
            If NumericUpDown3.Value > 0 Then
                Start_pause.Enabled = True
            End If
        End Sub

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Help me with this code.

    No, no, no. The -= operator is saying to decrease the current value by some amount:
    Code:
    NumericUpDown.Value -= 1
    'Is the same as:
    NumericUpDown.Value = NumericUpDown.Value - 1
    So you don't use the -= operator in conditional statements:
    Code:
    If NumericUpDown.Value -= 0 Then 'This is a no no!
    If NumericUpDown.Value = 0 Then 'This is correct
    You're also converting values to strings and then trying to set the Value property to a string value. The Value property accepts Doubles as values. So statements like this are wrong:
    Code:
    NumericUpDown2.Value -= CStr(CDbl(NumericUpDown2.Value) - 1)
    That should be:
    Code:
    NumericUpDown2.Value -= 1
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  13. #13

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Now there is no end statement problems, but only CShort(Timer3.Enabled) = False expression is a value and there fore cannot be the target of the assignment & option strict: no boolean to integer.
    Code:
     Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
            hours_label.Text = "00"
            minutes_label.Text = "00"
            seconds_label.Text = "00"
            Timer3.Enabled = False
        End Sub
        Private Sub Start_pause_Click(sender As Object, e As EventArgs) Handles Start_pause.Click
            If Start_pause.Text = "Start" Then
                Timer3.Enabled = True
                ok_button.Enabled = False
                Start_pause.Text = "Pause"
            ElseIf Start_pause.Text = "Pause" Then
                Timer3.Enabled = False
                Restart.Enabled = True
                ok_button.Enabled = True
                Start_pause.Text = "Start"
            End If
        End Sub
        Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
            Timer3.Interval = 1000
            If CBool(NumericUpDown1.Value) = 0 Then
                NumericUpDown1.Value = 0
            End If
            If NumericUpDown2.Value = 0 Then
                NumericUpDown2.Value = 0
            End If
            If NumericUpDown3.Value = 0 Then
                NumericUpDown3.Value = 0
            End If
            If NumericUpDown1.Value = 0 Then
                NumericUpDown1.Value = 0
            End If
            If NumericUpDown2.Value = 0 Then
                NumericUpDown2.Value = 0
            End If
            If NumericUpDown3.Value = 0 Then
                NumericUpDown3.Value = 0
            End If
            If NumericUpDown3.Value > 0 Then
                NumericUpDown3.Value -= 1
            End If
            If NumericUpDown2.Value > 0 Then
                If CBool(NumericUpDown3.Value) = 0 Then
                    NumericUpDown2.Value -= 1
                    NumericUpDown3.Value = 59
                End If
            End If
            If NumericUpDown1.Value > 0 Then
                If CBool(NumericUpDown2.Value) = 0 Then
                    If CBool(NumericUpDown3.Value) = 0 Then
                        NumericUpDown1.Value -= 1
                        NumericUpDown2.Value = 59
                        NumericUpDown3.Value = 59
                    End If
                End If
            End If
            If CBool(NumericUpDown3.Value) = 0 Then
                If CBool(NumericUpDown2.Value) = 0 Then
                    If NumericUpDown1.Value = 0 Then
                        CShort(Timer3.Enabled) = False
                        Beep()
                        MsgBox("Time is up")
                        Start_pause.Text = "Start"
                        Restart.Enabled = False
                    End If
                End If
            End If
        End Sub
        Private Sub ok_button_Click(sender As Object, e As EventArgs) Handles ok_button.Click
            If CBool(NumericUpDown1.Value) = 0 Then
                If CBool(NumericUpDown2.Value) = 0 Then
                    If CBool(NumericUpDown3.Value) = 0 Then
                        Start_pause.Enabled = False
                        MsgBox("Please enter a value higher then 0")
                    End If
                End If
            End If
            ok_button.Enabled = True
            hours_label.Text = CStr(NumericUpDown1.Value)
            minutes_label.Text = CStr(NumericUpDown2.Value)
            seconds_label.Text = CStr(NumericUpDown3.Value)
            If NumericUpDown1.Value > 0 Then
                Start_pause.Enabled = True
            End If
            If NumericUpDown2.Value > 0 Then
                Start_pause.Enabled = True
            End If
            If NumericUpDown3.Value > 0 Then
                Start_pause.Enabled = True
            End If
        End Sub

  14. #14
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Help me with this code.

    That's because you're converting the Enabled property of a Timer to a Short data type and then comparing it to a Boolean value. The Enabled property is a Boolean value, so just don't convert the Enabled property to a short:
    Code:
    Timer3.Enabled = False
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  15. #15

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    There is 9 boolean to integer option strict problems and one expression is a value and therfore ...

  16. #16

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Sorry, now just 9 boolean problems.

  17. #17
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: Help me with this code.

    Quote Originally Posted by Cow124 View Post
    There is 9 boolean to integer option strict problems and one expression is a value and therfore ...
    This line here:
    Code:
    If CBool(NumericUpDown1.Value) = 0 Then
    CBool converts an object to a Boolean value. Why are you converting a Double to a Boolean and then comparing it with an Integer? Quit converting objects that don't need to be converted!
    Code:
    If NumericUpDown1.Value = 0 Then
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  18. #18

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    I still have the same original problem (however better the code looks).

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Help me with this code.

    Quote Originally Posted by Cow124 View Post
    I still have the same original problem (however better the code looks).
    Perhaps you should show us the code as it is now, because if it looks better then it will be easier to read and therefore easier to identify where the issue is. Perhaps you could restate the issue clearly too. "I can't set the timer again" doesn't really cut it. Be PRECISE. Is an exception thrown when you call some method or set some property? That sort of thing. What EXACTLY happens that you don't expect? You might try providing meaningful titles for your threads in future too. I cam directly to your last post and had absolutely no idea what this thread was about because there was absolutely no information in the post to indicate the topic.

  20. #20

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Oops, I forgot the code and if you want to see the problem, look at the first part of the thread.
    Code:
        Private Sub Restart_Click(sender As Object, e As EventArgs) Handles Restart.Click
            hours_label.Text = "00"
            minutes_label.Text = "00"
            seconds_label.Text = "00"
            Timer3.Enabled = False
        End Sub
        Private Sub Start_pause_Click(sender As Object, e As EventArgs) Handles Start_pause.Click
            If Start_pause.Text = "Start" Then
                Timer3.Enabled = True
                ok_button.Enabled = False
                Start_pause.Text = "Pause"
            ElseIf Start_pause.Text = "Pause" Then
                Timer3.Enabled = False
                Restart.Enabled = True
                ok_button.Enabled = True
                Start_pause.Text = "Start"
            End If
        End Sub
        Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
            Timer3.Interval = 1000
            If NumericUpDown1.Value = 0 Then
                NumericUpDown1.Value = 0
            End If
            If NumericUpDown2.Value = 0 Then
                NumericUpDown2.Value = 0
            End If
            If NumericUpDown3.Value = 0 Then
                NumericUpDown3.Value = 0
            End If
            If NumericUpDown1.Value = 0 Then
                NumericUpDown1.Value = 0
            End If
            If NumericUpDown2.Value = 0 Then
                NumericUpDown2.Value = 0
            End If
            If NumericUpDown3.Value = 0 Then
                NumericUpDown3.Value = 0
            End If
            If NumericUpDown3.Value > 0 Then
                NumericUpDown3.Value -= 1
            End If
            If NumericUpDown2.Value > 0 Then
                If NumericUpDown3.Value = 0 Then
                    NumericUpDown2.Value -= 1
                    NumericUpDown3.Value = 59
                End If
            End If
            If NumericUpDown1.Value > 0 Then
                If NumericUpDown2.Value = 0 Then
                    If NumericUpDown3.Value = 0 Then
                        NumericUpDown1.Value -= 1
                        NumericUpDown2.Value = 59
                        NumericUpDown3.Value = 59
                    End If
                End If
            End If
            If NumericUpDown3.Value = 0 Then
                If NumericUpDown2.Value = 0 Then
                    If NumericUpDown1.Value = 0 Then
                        Timer3.Enabled = False
                        Beep()
                        MsgBox("Time is up")
                        Start_pause.Text = "Start"
                        Restart.Enabled = False
                    End If
                End If
            End If
        End Sub
        Private Sub ok_button_Click(sender As Object, e As EventArgs) Handles ok_button.Click
            If NumericUpDown1.Value = 0 Then
                If NumericUpDown2.Value = 0 Then
                    If NumericUpDown3.Value = 0 Then
                        Start_pause.Enabled = False
                        MsgBox("Please enter a value higher then 0")
                    End If
                End If
            End If
            ok_button.Enabled = True
            hours_label.Text = CStr(NumericUpDown1.Value)
            minutes_label.Text = CStr(NumericUpDown2.Value)
            seconds_label.Text = CStr(NumericUpDown3.Value)
            If NumericUpDown1.Value > 0 Then
                Start_pause.Enabled = True
            End If
            If NumericUpDown2.Value > 0 Then
                Start_pause.Enabled = True
            End If
            If NumericUpDown3.Value > 0 Then
                Start_pause.Enabled = True
            End If
        End Sub

  21. #21

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Is anyone home?

  22. #22

    Thread Starter
    Member Cow124's Avatar
    Join Date
    Aug 2014
    Posts
    36

    Re: Help me with this code.

    Quote Originally Posted by jmcilhinney View Post
    As I've already told you, a countdown timer is almost exactly the same as a stopwatch. You still use a Stopwatch to measure the passing of time and then you simply subtract the elapsed time from the total time to get the time remaining. For instance, if you want to countdown 10 minutes then you'd use:
    Code:
    Dim totalTime = TimeSpan.FromMinutes(10)
    To get the time remaining at any point, you do this:
    Code:
    Dim timeRemaining = totalTime - myStopwatch.Elapsed
    Displaying the time remaining is then just a matter of formatting a TimeSpan, as it is for displaying the elapsed time of a stopwatch.
    How would I replace the (10) with the time value entered in with the integers and what would I replace my Stopwatch with?

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