Welcome to Dream.In.Code
Become a VB Expert!

Join 149,478 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,528 people online right now. Registration is fast and FREE... Join Now!




Stop Watch

2 Pages V  1 2 >  
Reply to this topicStart new topic

Stop Watch, Creating a program that resembles a stop watch

Goddard
23 Apr, 2007 - 05:57 PM
Post #1

New D.I.C Head
*

Joined: 23 Apr, 2007
Posts: 25


My Contributions
My goal is to make a program that acts like a stop watch. I don't want it to reset the timer to 0 when I hit stop. Here's the Interface I'm using IPB Image

Problem: My seconds column works that way i want it to. The tenth and hundredth sections don't display single digits but display the fractions i created.


I have this defined in the form.
CODE
Dim stopLoop As Integer = 0
    Dim sW As Stopwatch = New Stopwatch


My start button so far.

CODE
  'start the clock
        Dim sec As Integer = Nothing
        Dim hsec As Double
        Dim tsec As Double
        

        'Reset the loop variable.
        stopLoop = 0

        'Start the stopWatch.
        sW.Start()

        Do
            My.Application.DoEvents()
            'Calculate Time
            sec = CInt(sW.ElapsedMilliseconds \ 1000)
            hsec = CInt(sW.ElapsedMilliseconds \ 10)
            tsec = CInt(sW.ElapsedMilliseconds \ 100)


            'Get the elapsed time in seconds.
            Me.lblSeconds.Text = sec.ToString
            Me.lblHundSeconds.Text = hsec.ToString
            Me.lblTenSeconds.Text = tsec.ToString

            'Continue to loop until the stop button is pushed.
        Loop Until stopLoop = 1


My Exit Button so far

CODE
'Stop the clock

        'Cause the loop to stop.
        stopLoop = 1

        'Turn off the stopWatch.
        sW.Stop()


My Reset button will be easy and I'll just string.empty everything.

Thank You for the help

Note - I have also included the project as an attachment. - Note





Attached File(s)
Attached File  Stop_Watch.zip ( 55.85k ) Number of downloads: 390
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Stop Watch
23 Apr, 2007 - 07:47 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Can you provide an example of the output you want?

Or at least describe the problem a little more. I'm not sure what you are asking for.
User is online!Profile CardPM
+Quote Post

Goddard
RE: Stop Watch
24 Apr, 2007 - 12:10 PM
Post #3

New D.I.C Head
*

Joined: 23 Apr, 2007
Posts: 25


My Contributions
I want it to look like this when it runs.
IPB Image
IPB Image

But it looks like this.

IPB Image
User is offlineProfile CardPM
+Quote Post

roadfury
RE: Stop Watch
24 Apr, 2007 - 12:55 PM
Post #4

New D.I.C Head
Group Icon

Joined: 5 Feb, 2007
Posts: 11


My Contributions
Give this a try (I haven't tested it, so there's no guarantee):

CODE

sec = CInt(Int(sW.ElapsedMilliseconds \ 1000))
tsec = CInt(Int(sW.ElapsedMilliseconds - (sec * 1000)) \ 100)
hsec = CInt((sW.ElapsedMilliseconds - (sec * 1000) - (tsec * 100)) \ 10)

User is offlineProfile CardPM
+Quote Post

Goddard
RE: Stop Watch
24 Apr, 2007 - 01:11 PM
Post #5

New D.I.C Head
*

Joined: 23 Apr, 2007
Posts: 25


My Contributions
roadfury, your amazing.

CODE
sec = CInt(Int(sW.ElapsedMilliseconds \ 1000))
tsec = CInt(Int(sW.ElapsedMilliseconds - (sec * 1000)) \ 100)

works great but on the third equation
CODE
hsec = CInt((sW.ElapsedMilliseconds - (sec * 1000) - (tsec * 100)) \ 10)

Visual Basic kept giving me an error with Option Strict, so I changed
CODE
Dim hsec As Double
        Dim tsec As Double

To
CODE
  Dim hsec As Long
        Dim tsec As Long


I just realized that when I start the application, no matter where I click, the first mouse click doesn't do anything.

Lets say I start the stopwatch and I go to click stop, the click doesn't do anything, but if i click on the second time the buttons work fine.

I might not be making sense blink.gif

This post has been edited by Goddard: 24 Apr, 2007 - 01:12 PM
User is offlineProfile CardPM
+Quote Post

Goddard
RE: Stop Watch
24 Apr, 2007 - 02:38 PM
Post #6

New D.I.C Head
*

Joined: 23 Apr, 2007
Posts: 25


My Contributions
I just got to thinking, which is what I do biggrin.gif

An alternative to using the stopwatch I might try is to use some loops.

Here's how I plan to embark on this alternative:
1. Code the START button to cause the application to begin counting by 1/100's of a second.
2. When 10 of these have accumulated increment the 1/10's of a second accumulator.
3. When ten 1/10's of a second has accumulated increment the seconds' accumulator.

So basically, When the counter for 1/100's reaches 10 it adds 1 to the 1/10's. And when 1/10's reaches 10 it adds one second.

I might try doing this with a For...Next statement. Is DO...Until a better idea?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Stop Watch
24 Apr, 2007 - 10:31 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,302



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
If you know exactly how many times the loop needs to iterate use a For loop. If the loop needs to iterate until a condition is met, but you don't know how many times that may be, then use either a Do Until, Do While or a While loop.
User is online!Profile CardPM
+Quote Post

Goddard
RE: Stop Watch
30 Apr, 2007 - 11:09 AM
Post #8

New D.I.C Head
*

Joined: 23 Apr, 2007
Posts: 25


My Contributions
The more I look at it, the less I have the need to do all loops.

So, i officially say this stop watch has been successfully completed.


This post has been edited by Goddard: 30 Apr, 2007 - 11:18 AM
User is offlineProfile CardPM
+Quote Post

Goddard
RE: Stop Watch
30 Apr, 2007 - 11:17 AM
Post #9

New D.I.C Head
*

Joined: 23 Apr, 2007
Posts: 25


My Contributions
Thank You for all the help in designing this stop watch. I am now going to post the final code here for easy reference.

CODE
Public Class MainForm
    Dim stopLoop As Integer = 0
    Dim sW As Stopwatch = New Stopwatch

Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        'start the clock
        Dim sec As Integer = Nothing
        Dim hsec As Long
        Dim tsec As Long
        

        'Reset the loop variable.
        stopLoop = 0

        'Start the stopWatch.
        sW.Start()

        'Focus on Stop
        Me.btnStop.Focus()

        Do
            My.Application.DoEvents()
            'Calculate Time
            sec = CInt(Int(sW.ElapsedMilliseconds \ 1000))
            tsec = CInt(Int(sW.ElapsedMilliseconds - (sec * 1000)) \ 100)
            hsec = CInt((sW.ElapsedMilliseconds - (sec * 1000) - (tsec * 100)) \ 10)


            'Get the elapsed time in seconds.
            Me.lblSeconds.Text = sec.ToString
            Me.lblHundSeconds.Text = hsec.ToString
            Me.lblTenSeconds.Text = tsec.ToString

            'Continue to loop until the stop button is pushed.
        Loop Until stopLoop = 1

    End Sub

Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click


        'Stop the clock

        'Cause the loop to stop.
        stopLoop = 1

        'Turn off the stopWatch.
        sW.Stop()


    End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        'Reset the loop variable.
        stopLoop = 0

        'Reset the stopWatch to 00:00:00.
        sW.Reset()

        Me.lblSeconds.Text = "0"
        Me.lblTenSeconds.Text = "0"
        Me.lblHundSeconds.Text = "0"

    End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'close form
        Me.Close()

    End Sub

End Class

User is offlineProfile CardPM
+Quote Post

kaufman
RE: Stop Watch
30 Apr, 2007 - 11:38 AM
Post #10

New D.I.C Head
Group Icon

Joined: 29 Apr, 2007
Posts: 37


Dream Kudos: 25
My Contributions
heheh sorry i couldnt helped. anyways props to ya very well assembled code. you may want to document it though so other people dont have to go through what you did.
User is offlineProfile CardPM
+Quote Post

Goddard
RE: Stop Watch
30 Apr, 2007 - 11:45 AM
Post #11

New D.I.C Head
*

Joined: 23 Apr, 2007
Posts: 25


My Contributions
Newbie question, how would i document it?

I went to edit the first post but the edit button doesn't show up. mad.gif
User is offlineProfile CardPM
+Quote Post

kaufman
RE: Stop Watch
30 Apr, 2007 - 01:55 PM
Post #12

New D.I.C Head
Group Icon

Joined: 29 Apr, 2007
Posts: 37


Dream Kudos: 25
My Contributions
umm i dunno if your asking the question im thinking you are asking so ill answer two.
1)
Documenting is just adding comments so other people can recreate your program with a little bit of different
code.
2)
I know when i edit my posts alot it dosent let me edit it again, maybe theres some kind of edit limit.



Hey dont worry about noob questions though. I shouldnt be answering them im a noob too.

~~~kaufman~~~

This post has been edited by kaufman: 30 Apr, 2007 - 01:58 PM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month