Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating a Stop Watch in VB.Net

Author: DevASP
Download Source Code : 335_Timer.zip

This article is about how you can create a stop watch in VB.Net using the timer control.

  • Steps that you will do.  
  • Open visual studio and create a new window application. 
  • Drop four text fields on the form and set their following properties.

First text box.

Name=tbHours

 

Second text box.

Name=tbMin

 

Third text box.

Name=tbSec

 

Fourth text box.

Name=tbMsec

 

  • Now drop five label controls and set their properties as below:

 

First label

Text = Hours

 

Second label

Text = Minutes

 

Third label

Text =Second

 

Four label

Text = Mili Seconds

 

Fifth label

Name = lbMessege

Text = “Are you sure you want to reset the timer”

  

  • Now drop five button controls and set their properties as below:

 

First button

Text = btStart

Name = Start

 

Second button

Text = btStop

Name = Stop

 

Third button

Text = btReset

Name = Reset

 

Fourth button

Text = btOk

Name = Ok

 

Fifth button

Text = btCancel

Name = Cancel

 

  • Place a timer control in the designer of your form. 
  • Open your code window and write the following code. 
  • Declare following variables.

Dim tmr1 As Integer

Dim a, b, c, d As Integer

 

  •  Write following code in the click event of btStart

Timer1.Enabled = True

btStop.Enabled = True

btStart.Enabled = False

 

  •  Write following code in the click event of the btStop.

Timer1.Enabled = True

btStop.Enabled = True

btStart.Enabled = False

  • Write following code in the timer tick event.

tmr1 = tmr1 + 1

tbMsec.Text = tmr1

If tmr1 = 10 Then

a = a + 1

tmr1 = 0

End If

If a = 60 Then

b = b + 1

a = 0

End If

If b = 60 Then

c = c + 1

b = 0

End If

tbSec.Text = a

tbMin.Text = b

tbHours.Text = c

  • Write following code in the load event of form.

a = 0

b = 0

c = 0

d = 0

tbHours.ReadOnly = True

tbMin.ReadOnly = True

tbSec.ReadOnly = True

tbMsec.ReadOnly = True

  • Write following code in the click event of the btStop.

Timer1.Enabled = False

btReset.Enabled = True

btStop.Enabled = False

btStart.Enabled = True

  • Write following code in the click event of the btReset.

tbMessege.Visible = True

btOk.Visible = True

btCancel.Visible = True

btReset.Enabled = False

  • Write following code in the click event of btOk.

tbMsec.Text = 0

tbSec.Text = 0

tbMin.Text = 0

tbHours.Text = 0

a = 0

b = 0

c = 0

d = 0

tbMessege.Visible = False

btOk.Visible = False

btCancel.Visible = False

  • Write following code in the click event of the btCancel.

tbMessege.Visible = False

btOk.Visible = False

btCancel.Visible = False

btReset.Enabled = True

 

Article Comments
i'm very thanks , and good job

Posted on 11/17/2009 5:16:13 PM by MidNight72

your code are so good

Posted on 11/24/2009 4:27:09 AM by sati

thanks, very nice!...good job...!.

Posted on 1/17/2010 12:10:33 AM by amir

Thanks a lot.your code is helpful for me.

Posted on 1/19/2010 9:31:54 AM by Suvarna

a very good and understandable code

Posted on 2/20/2010 2:12:24 PM by aasim

few spelling mistake but it's just great

Posted on 3/7/2010 6:10:21 AM by Night_Cry

thank you so much

Posted on 3/8/2010 4:04:05 PM by Ward

Hey where will I put the code: Dim tmr1 As Integer

Dim a, b, c, d As Integer?
and, why does there still errors. I have already edit the names.

Posted on 3/17/2010 9:30:39 AM by Heaven

This code was really useful and very simple to use. This is the code i was searching for the past two weeks and You helped me out. Thanks a lot.:)

Posted on 5/7/2010 2:36:57 AM by Nalini

They way it should be done:

Public Class frmMain

Dim testWatch As New System.Diagnostics.Stopwatch

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Try
clearLabels()
tmrStopWatch.Enabled = False
btnStop.Enabled = False
btnStart.Enabled = True
btnReset.Enabled = False
Catch ex As Exception

End Try

End Sub

Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

Try
If testWatch.IsRunning = True Then
testWatch.Stop()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

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

testWatch.Start()
tmrStopWatch.Enabled = True
btnStop.Enabled = True
btnStart.Enabled = False
btnReset.Enabled = False

End Sub

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

testWatch.Stop()
tmrStopWatch.Enabled = False
btnStop.Enabled = False
btnStart.Enabled = True
btnReset.Enabled = True

End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click

If testWatch.IsRunning = True Then
testWatch.Stop()
tmrStopWatch.Enabled = False
End If

testWatch.Reset()
clearLabels()
tmrStopWatch.Enabled = False
btnStart.Enabled = True
btnStop.Enabled = False
btnReset.Enabled = False

End Sub

Private Sub clearLabels()

lblMiliSeconds.Text = "0"
lblSeconds.Text = "0"
lblMinutes.Text = "0"
lblHours.Text = "0"

End Sub

Private Sub tmrStopWatch_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrStopWatch.Tick

lblMiliSeconds.Text = testWatch.ElapsedMilliseconds
lblSeconds.Text = testWatch.Elapsed.Seconds
lblMinutes.Text = testWatch.Elapsed.Minutes
lblHours.Text = testWatch.Elapsed.Hours

End Sub

End Class

Posted on 7/3/2010 2:08:12 PM by Mike T.

is there any screenshots?

Posted on 7/7/2010 5:24:49 AM by Jay

after looking at your program only i am interested to study vb.net. It is simple and understandable.Thank you for this help.

Posted on 8/28/2010 6:16:45 AM by saranya

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Change Mouse Cursor in VB.Net Applications

Disclaimer - Privacy
© 2002-2017 DevASP.net