..


Sponsored Links

A timer in VB6

Article written by Luca Ruggiero

A stopwatch is a tool that ... oh well, I do not think it is appropriate to explain what a stopwatch rather should understand how to make a Visual Basic 6.

The functions are very simple: a Start button that will stop the stopwatch will be started soon, counting the seconds from the time meanwhile.

Clicking the button again while its caption is Stop, the clock will reset itself and stop.

We create a new EXE in our copy of VB6 and create a form, adding a label, a command and a timer:

Assign the label has the name, the name of the button control and assign the name to the timer time.

We double-click the timer and add the following code:






 Private Sub tempo_Timer ()



    



 conta.Caption conta.Caption + 1 =







 End Sub



Just go to increase the value of the label of a unit.

The more complex the code be applied to the button:






 Private Sub comando_Click ()



    



 If comando.Caption = "Start" Then



        



 tempo.Enabled = True



        



 comando.Caption = "Stop"



        



 Call tempo_Timer



        



 tempo.Interval = 1000



    



 Else



        



 tempo.Enabled = False



        



 comando.Caption = "Start"



        



 tempo.Interval = 0



        



 conta.Caption = 0



    



 End If







 End Sub



We check first that his is capition Start, in which case we go, in order:
  • Enable the timer to run;
  • rename the caption of the button from Start to Stop;
  • to call the function that increments the timer;
  • to set a second (1000) interval timer running.
At this point the clock is gone and will look as follows:

Click again to Stop, at run time, the program will enter the Else conditional block, and perform the following functions:
  • disable the timer;
  • changing the caption of the button from Stop to Start;
  • to stop the interval;
  • to reset the stopwatch value.

In the same category ...
E-Learning
MS Access (Advanced) MS Access (Advanced)
Learn how to create and manage databases quickly and easily. Starting from 29 €.
Visual Basic 6 (Course) Visual Basic 6 (Course)
Make Desktop Applications with VB6. From 39 €.
Sponsored Links