Stop Watch

Creating a program that resembles a stop watch

Page 1 of 1

14 Replies - 16340 Views - Last Post: 02 May 2007 - 09:26 PM Rate Topic: -----

#1 Goddard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 23-April 07

Stop Watch

Posted 23 April 2007 - 06:57 PM

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 Posted 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.
Dim stopLoop As Integer = 0
	Dim sW As Stopwatch = New Stopwatch


My start button so far.

  '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

'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)



Is This A Good Question/Topic? 0
  • +

Replies To: Stop Watch

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Stop Watch

Posted 23 April 2007 - 08:47 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 Goddard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 23-April 07

Re: Stop Watch

Posted 24 April 2007 - 01:10 PM

I want it to look like this when it runs.
Posted Image
Posted Image

But it looks like this.

Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 roadfury  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 52
  • Joined: 05-February 07

Re: Stop Watch

Posted 24 April 2007 - 01:55 PM

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

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


Was This Post Helpful? 0
  • +
  • -

#5 Goddard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 23-April 07

Re: Stop Watch

Posted 24 April 2007 - 02:11 PM

roadfury, your amazing.

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

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

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

To
  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:

This post has been edited by Goddard: 24 April 2007 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#6 Goddard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 23-April 07

Re: Stop Watch

Posted 24 April 2007 - 03:38 PM

I just got to thinking, which is what I do :D

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?
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Stop Watch

Posted 24 April 2007 - 11:31 PM

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.
Was This Post Helpful? 0
  • +
  • -

#8 Goddard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 23-April 07

Re: Stop Watch

Posted 30 April 2007 - 12:09 PM

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 April 2007 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#9 Goddard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 23-April 07

Re: Stop Watch

Posted 30 April 2007 - 12:17 PM

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

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

Was This Post Helpful? 1

#10 kaufman  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 29-April 07

Re: Stop Watch

Posted 30 April 2007 - 12:38 PM

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.
Was This Post Helpful? 0
  • +
  • -

#11 Goddard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 23-April 07

Re: Stop Watch

Posted 30 April 2007 - 12:45 PM

Newbie question, how would i document it?

I went to edit the first post but the edit button doesn't show up. :angry:
Was This Post Helpful? 0
  • +
  • -

#12 kaufman  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 29-April 07

Re: Stop Watch

Posted 30 April 2007 - 02:55 PM

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 April 2007 - 02:58 PM

Was This Post Helpful? 0
  • +
  • -

#13 TheNewGuy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 07

Re: Stop Watch

Posted 02 May 2007 - 02:36 PM

Well, he did document what he was doing. note the lines with ' at the beginning

Good work
Was This Post Helpful? 0
  • +
  • -

#14 sidhartha says  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-May 07

Re: Stop Watch

Posted 02 May 2007 - 08:49 PM

Ok, Goddard and I are in the same class. We have been working on mine, and starting with the system clock, we ended up with this:
Public Class MainForm
	Dim tmr1 As Integer
	Dim milsec As Integer
	Dim sec As Integer
   
	Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
		Timer1.Enabled = True
		Me.btnStop.Focus()
	End Sub

	Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
		Timer1.Enabled = False
	End Sub

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		tmr1 = tmr1 + 1
		milsec = tmr1

		If tmr1 = 10 Then
			sec = sec + 1
			tmr1 = 0
		End If

		Me.lblHundreths.Text = CStr(Fix((milsec - milsec / 10) - milsec / 10))
		Me.lblTenths.Text = CStr(milsec)
		Me.lblSeconds.Text = CStr(sec)


		
	End Sub

	Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		sec = 0
		Me.lblSeconds.Text = "0"
		Me.lblTenths.Text = "0"
		Me.lblHundreths.Text = "0"
	   
	End Sub

	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Me.Close()
	End Sub

	Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
		tmr1 = 0
		sec = 0
		Me.lblSeconds.Text = "0"
		Me.lblTenths.Text = "0"
		Me.lblHundreths.Text = "0"
	End Sub
End Class


The code works fine, except for one thing. When the seconds reaches 10, it drops the ones digit and tracks the tens digit, so it seems like the seconds timer is stick, when in reality it is just waiting for the ones to go 1-9. Any help is greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#15 Goddard  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 23-April 07

Re: Stop Watch

Posted 02 May 2007 - 09:26 PM

I might sound silly, but did you try expanding the label for the seconds timer?

So it might look something like this.

Posted Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1