Results 1 to 9 of 9

Thread: [RESOLVED] [2005] Digital Stopwatch

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Resolved [RESOLVED] [2005] Digital Stopwatch

    Hi All,

    Like the question says, I want to create a Digital Stopwatch.
    I have 4 pictureboxes in my form and a Timer1.
    For the Leds I've been drawing 10 Images who representing the Leds from 0 to 9.

    I've tryed to run my project with the Selected Case method (who's working ) the problem with this method is that I can't start it from the beginning again.

    For example when I run this project and stop at 55 seconds, rset it and start it again then it starts from 55 seconds.

    How can I resolve this problem and is the Select Case method the best way to do this?

    Thanks in advance,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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

    Re: [2005] Digital Stopwatch

    I think we're going to have to see some code, but this sounds like a case where you need to have a function that resets everything back to the original state. Perhaps you could post the reset code you currently have.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Digital Stopwatch

    Quote Originally Posted by Shaggy Hiker
    I think we're going to have to see some code, but this sounds like a case where you need to have a function that resets everything back to the original state. Perhaps you could post the reset code you currently have.
    Hi,

    Here's my code, I think there will be better ways of doing it but it runs:

    Code:
    Private Sub Leds()
            Static Dim Led As Integer
             Static Dim Led1 As Integer
            Static Dim Led2 As Integer
            Select Case Led
                Case 0
                    PictureBox6.Image = My.Resources.Led1
                    Led += 1
                Case 1
                    PictureBox6.Image = My.Resources.Led2
                    Led += 1
                Case 2
                    PictureBox6.Image = My.Resources.Led3
                    Led += 1
                Case 3
                    PictureBox6.Image = My.Resources.Led4
                    Led += 1
                Case 4
                    PictureBox6.Image = My.Resources.Led5
                    Led += 1
                Case 5
                    PictureBox6.Image = My.Resources.Led6
                    Led += 1
                Case 6
                    PictureBox6.Image = My.Resources.Led7
                    Led += 1
                Case 7
                    PictureBox6.Image = My.Resources.Led8
                    Led += 1
                Case 8
                    PictureBox6.Image = My.Resources.Led9
                    Led += 1
                Case 9
                    PictureBox6.Image = My.Resources.Led0
                    Led = 0
                    Select Case Led1
                        Case 0
                            PictureBox5.Image = My.Resources.Led1
                            Led1 += 1
                        Case 1
                            PictureBox5.Image = My.Resources.Led2
                            Led1 += 1
                        Case 2
                            PictureBox5.Image = My.Resources.Led3
                            Led1 += 1
                        Case 3
                            PictureBox5.Image = My.Resources.Led4
                            Led1 += 1
                        Case 4
                            PictureBox5.Image = My.Resources.Led5
                            Led1 += 1
                        Case 5
                            PictureBox5.Image = My.Resources.Led0
                            Led1 = 0
                            Select Case Led2
                                Case 0
                                    PictureBox4.Image = My.Resources.Led1
                                    Led2 += 1
                                Case 1
                                    PictureBox4.Image = My.Resources.Led2
                                    Led2 += 1
                                Case 2
                                    PictureBox4.Image = My.Resources.Led3
                                    Led += 1
                                Case 3
                                    PictureBox4.Image = My.Resources.Led4
                                    Led2 += 1
                                Case 4
                                    PictureBox4.Image = My.Resources.Led5
                                    Led2 += 1
                                Case 5
                                    PictureBox4.Image = My.Resources.Led6
                                    Led2 += 1
                                Case 6
                                    PictureBox4.Image = My.Resources.Led7
                                    Led2 += 1
                                Case 7
                                    PictureBox4.Image = My.Resources.Led8
                                    Led2 += 1
                                Case 8
                                    PictureBox4.Image = My.Resources.Led9
                                    Led2 += 1
                                Case 9
                                    PictureBox4.Image = My.Resources.Led0
                                    Led2 = 0
                            End Select
                    End Select
            End Select
        End Sub
    
        Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            Timer1.Enabled = True
    
        End Sub
    
        Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
            PictureBox6.Image = My.Resources.Led0
            PictureBox5.Image = My.Resources.Led0
            PictureBox4.Image = My.Resources.Led0
            PictureBox3.Image = My.Resources.Led0
            PictureBox2.Image = My.Resources.Led0
            PictureBox1.Image = My.Resources.Led0
            ' <- here's is something missing to reset the selected case
        End Sub
    
        Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
            Timer1.Enabled = False
        End Sub
    End Class
    Thanks,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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

    Re: [2005] Digital Stopwatch

    Looks pretty straightforward, but I notice that the whole Select hinges on the static LED. Once you press reset, you set all the LEDs back to 0, but LED still holds the value it last held. Seems to me that in that case, the next time the timer fires, you will undo the reset.

    I think you probably have to elevate the LED variable to form scope, rather than keeping it as a static local variable. Then, you could add an LED=0 line to the reset code.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Digital Stopwatch

    Quote Originally Posted by Shaggy Hiker
    Looks pretty straightforward, but I notice that the whole Select hinges on the static LED. Once you press reset, you set all the LEDs back to 0, but LED still holds the value it last held. Seems to me that in that case, the next time the timer fires, you will undo the reset.

    I think you probably have to elevate the LED variable to form scope, rather than keeping it as a static local variable. Then, you could add an LED=0 line to the reset code.
    Hi,

    How can I do that and what should I put in the Timer event that it execute the variable.

    Thanks,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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

    Re: [2005] Digital Stopwatch

    Just move the LED variable outside of the Sub, and change it from Static Dim to Private. Doesn't really matter where it is, as long as it is in the class, and not in a sub.

    As for the timer event, I don't actually see what you are doing on the timer event already. I sort of assumed that you were just calling Leds(), but you didn't post it. Moving the variable outside of the sub won't affect the behavior of the sub in any way, but it will allow other subs to "see" the variable. Therefore, while you can't set it to 0 in the reset sub now, because the variable isn't visible to the reset sub, once you elevate the variable to form scope, then the reset sub will be able to set it to 0.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Digital Stopwatch

    Quote Originally Posted by Shaggy Hiker
    Just move the LED variable outside of the Sub, and change it from Static Dim to Private. Doesn't really matter where it is, as long as it is in the class, and not in a sub.

    As for the timer event, I don't actually see what you are doing on the timer event already. I sort of assumed that you were just calling Leds(), but you didn't post it. Moving the variable outside of the sub won't affect the behavior of the sub in any way, but it will allow other subs to "see" the variable. Therefore, while you can't set it to 0 in the reset sub now, because the variable isn't visible to the reset sub, once you elevate the variable to form scope, then the reset sub will be able to set it to 0.
    Hi,

    It could be that I did something wrong but it doesn't working, here's is my entire code:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            PictureBox6.Image = My.Resources.Led0
            PictureBox5.Image = My.Resources.Led0
            PictureBox4.Image = My.Resources.Led0
            PictureBox3.Image = My.Resources.Led0
            PictureBox2.Image = My.Resources.Led0
            PictureBox1.Image = My.Resources.Led0
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Leds()
            
        End Sub
        Private Led As Integer
        Private Led1 As Integer
        Private Led2 As Integer
        Private Sub Leds()
            Select Case Led
                Case 0
                    PictureBox6.Image = My.Resources.Led1
                    Led += 1
                Case 1
                    PictureBox6.Image = My.Resources.Led2
                    Led += 1
                Case 2
                    PictureBox6.Image = My.Resources.Led3
                    Led += 1
                Case 3
                    PictureBox6.Image = My.Resources.Led4
                    Led += 1
                Case 4
                    PictureBox6.Image = My.Resources.Led5
                    Led += 1
                Case 5
                    PictureBox6.Image = My.Resources.Led6
                    Led += 1
                Case 6
                    PictureBox6.Image = My.Resources.Led7
                    Led += 1
                Case 7
                    PictureBox6.Image = My.Resources.Led8
                    Led += 1
                Case 8
                    PictureBox6.Image = My.Resources.Led9
                    Led += 1
                Case 9
                    PictureBox6.Image = My.Resources.Led0
                    Led = 0
                    Select Case Led1
                        Case 0
                            PictureBox5.Image = My.Resources.Led1
                            Led1 += 1
                        Case 1
                            PictureBox5.Image = My.Resources.Led2
                            Led1 += 1
                        Case 2
                            PictureBox5.Image = My.Resources.Led3
                            Led1 += 1
                        Case 3
                            PictureBox5.Image = My.Resources.Led4
                            Led1 += 1
                        Case 4
                            PictureBox5.Image = My.Resources.Led5
                            Led1 += 1
                        Case 5
                            PictureBox5.Image = My.Resources.Led0
                            Led1 = 0
                            Select Case Led2
                                Case 0
                                    PictureBox4.Image = My.Resources.Led1
                                    Led2 += 1
                                Case 1
                                    PictureBox4.Image = My.Resources.Led2
                                    Led2 += 1
                                Case 2
                                    PictureBox4.Image = My.Resources.Led3
                                    Led += 1
                                Case 3
                                    PictureBox4.Image = My.Resources.Led4
                                    Led2 += 1
                                Case 4
                                    PictureBox4.Image = My.Resources.Led5
                                    Led2 += 1
                                Case 5
                                    PictureBox4.Image = My.Resources.Led6
                                    Led2 += 1
                                Case 6
                                    PictureBox4.Image = My.Resources.Led7
                                    Led2 += 1
                                Case 7
                                    PictureBox4.Image = My.Resources.Led8
                                    Led2 += 1
                                Case 8
                                    PictureBox4.Image = My.Resources.Led9
                                    Led2 += 1
                                Case 9
                                    PictureBox4.Image = My.Resources.Led0
                                    Led2 = 0
                            End Select
                    End Select
            End Select
        End Sub
    
        Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            Timer1.Enabled = True
    
        End Sub
    
        Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
            PictureBox6.Image = My.Resources.Led0
            PictureBox5.Image = My.Resources.Led0
            PictureBox4.Image = My.Resources.Led0
            PictureBox3.Image = My.Resources.Led0
            PictureBox2.Image = My.Resources.Led0
            PictureBox1.Image = My.Resources.Led0
           
        End Sub
    
        Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
            Timer1.Enabled = False
        End Sub
    End Class
    Thanks,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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

    Re: [2005] Digital Stopwatch

    You left these lines out of both the Load event, and the reset button code:

    Led=0
    Led1=0
    led2=0

    That should be sufficient. Without this, then those three variables will remain where they were when you pressed the stop button. Pressing reset would set the pictureboxes back....at least I would expect them to, but I have never worked with PictureBox controls.....but would not reset the count. That would mean that as soon as you press start again, you would start up from the point you left off. By resetting those variables to 0 in the reset section, that should be cleared up.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Digital Stopwatch

    Hi,

    Thanks, I've learned that you can and how to reset a Select Case and that I was very close to accomplish this project.
    In attachment you can see my Digital Stopwatch


    Wkr,

    sparrow1
    Last edited by sparrow1; Apr 8th, 2007 at 11:40 AM.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

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