All About Visual Basic 6 Programming

Creating a Stop Watch

Posted by Psycho Programer On Saturday, December 25, 2010 2 comments

Click File >> New Project. You might be asked to save changes to the current project if you’ve been experimenting. If so, click the No button. If you want to keep your work, click Yes. Select Standard EXE from the New Project Window .

Standard EXE

Select the Form, change its Caption property to Stop Watch  and MaxButton to False.

Insert 6 TextBox, a Command Button and a Timer on the form. Arrange the form as shown in figure. The text Hour(s), Minute(s) and Second(s)  are written using a small text box  inside a bigger TextBox. The name of the Bigger TextBox are txtHour, txtMinute and txtSecond  respectively to display Hour, Minute and Second. The Smaller Text Box inside txtHour, txtMinute and txtSecond is named as txtHr, txtMin and txtSec repectively.

Name Command Button as cmdStartStop and Timer as tmrTimer. Set the Caption property of cmdStartStop to Start/Stop.

Design

Now, Double-Click on the Form to open code window with Form Load event. Copy the following code inside the Form Load event to change Font, FontSize, Alignment and other properties.

   1:  Private Sub Form_Load()
   2:  txtHour.Font = "Lucidia Console"
   3:  txtHour.FontSize = 55
   4:  txtHour.Alignment = vbCenter
   5:  txtHour.Text = "0"
   6:  txtHour.Enabled = False
   7:  txtHr.FontBold = True
   8:   
   9:  txtMinute.Font = "Lucidia Console"
  10:  txtMinute.FontSize = 55
  11:  txtMinute.Alignment = vbCenter
  12:  txtMinute.Text = "0"
  13:  txtMinute.Enabled = False
  14:  txtMin.FontBold = True
  15:   
  16:  txtSecond.Font = "Lucidia Console"
  17:  txtSecond.FontSize = 55
  18:  txtSecond.Alignment = vbCenter
  19:  txtSecond.Text = "0"
  20:  txtSecond.Enabled = False
  21:  txtSec.FontBold = True
  22:   
  23:  tmrTimer.Enabled = False
  24:  End Sub

The statement on line number 5, 12 and 19 sets the text of the TextBox to Zero and the statement on line number 6, 13 and 20 disable the controls so that user cannot access these controls. Line #23 enables the Timer.

Set the Interval property of Timer tmrTimer to 1000 and paste the following code on the Timer event of tmrTimer. The code inside this Timer event will execute in every 1 second as defined by its Interval property.

   1:  Private Sub tmrTimer_Timer()
   2:  txtSecond.Text = txtSecond.Text + 1
   3:  If txtSecond.Text > 59 Then
   4:   txtMinute.Text = txtMinute.Text + 1
   5:   txtSecond.Text = "0"
   6:   If txtMinute.Text > 59 Then
   7:    txtHour.Text = txtHour.Text + 1
   8:    txtMinute.Text = "0"
   9:   End If
  10:  End If
  11:  End Sub

The statement on line #2 increase the text of the txtSecond by 1 (Every Second). When the text on txtSecond is greater than 59, the text of txtMinute TextBox is increased by 1 and the txtSecond is reset to Zero. Similarly when the text on txtMinute is greater than 59, the text of txtHour is increased by 1 and the txtMinute is reset to Zero.

Now, Double-Click the cmdStartStop button and write following code.

   1:  Private Sub cmdStartStop_Click()
   2:  If tmrTimer.Enabled Then
   3:   tmrTimer.Enabled = False
   4:  Else
   5:   tmrTimer.Enabled = True
   6:  End If
   7:  End Sub

The above line of statements disable the Timer if it is enabled and vice-versa.

Run the program by pressing F5 from the keyboard. Click Start/Stop button to Start and Stop the Stop Watch.

Run

2 comments:

Mani said...

If i want to reset,see the current time and register the lap time. Then How?Plz help

Unknown said...

Thank you so much for this :)

Post a Comment