VB6 and VB.NET implementation with interactive demo
This digital stopwatch application demonstrates how to create a functional timer with multiple modes (stopwatch, clock, and date display) using Visual Basic. The application features:
Precision timing with start, stop, and reset controls
Real-time display of the current time
Current date display
Easy transition between different display modes
We'll explore both VB6 and VB.NET implementations of this application, highlighting the differences and similarities between the two versions.
| Feature | VB6 Implementation | VB.NET Implementation |
|---|---|---|
| Timer Component | Timer control with Interval property (1 = 1ms) | System.Windows.Forms.Timer with Interval property (1 = 1ms) |
| Time Calculation | Manual calculation using multiple variables | TimeSpan structure for easier time calculations |
| UI Elements | Label for display, Command Buttons for controls | Label for display, Button controls for actions |
| Event Handling | Timer_Timer event for stopwatch, Timer2 for clock | Single Timer with Tick event for all timing needs |
| Date/Time Display | Date function for date, Time function for clock | DateTime.Now property for both date and time |
| Code Structure | Procedural with global variables | More object-oriented with local variables |
Dim a As String
Dim b As String
Dim c As String
Dim x As String
Dim y As String
Dim z As String
Dim u As String
Dim v As String
Private Sub Form_Load()
ResetDisplay
End Sub
Private Sub clock_Click()
Timer1.Enabled = False
Timer2.Enabled = True
End Sub
Private Sub date_Click()
Label1.Caption = Date
Timer2.Enabled = False
End Sub
Private Sub stopwc_Click()
Timer2.Enabled = False
ResetDisplay
End Sub
Private Sub Command1_Click() ' Start
Timer1.Enabled = True
Timer1.Interval = 1
End Sub
Private Sub Command2_Click() ' Stop
Timer1.Enabled = False
End Sub
Private Sub Command3_Click() ' Reset
Timer1.Enabled = False
ResetDisplay
End Sub
Private Sub Timer1_Timer()
counttime
End Sub
Private Sub Timer2_Timer()
Label1.Caption = Time
End Sub
Sub counttime()
If Val(v) < 9 Then
v = v + 1
ElseIf Val(u) < 9 Then
v = 0
u = u + 1
ElseIf Val(z) < 9 Then
v = 0
u = 0
z = z + 1
ElseIf Val(y) < 5 Then
v = 0
u = 0
z = 0
y = y + 1
ElseIf Val(x) < 9 Then
v = 0
u = 0
z = 0
y = 0
x = x + 1
ElseIf Val(c) < 5 Then
v = 0
u = 0
z = 0
y = 0
x = 0
c = c + 1
ElseIf Val(b) < 9 Then
v = 0
u = 0
z = 0
y = 0
x = 0
c = 0
b = b + 1
ElseIf Val(a) < 9 Then
v = 0
u = 0
z = 0
y = 0
x = 0
c = 0
b = 0
a = a + 1
End If
Label1.Caption = a & b & ":" & c & x & ":" & y & z & "." & u & v
End Sub
Sub ResetDisplay()
a = "0": b = "0": c = "0": x = "0"
y = "0": z = "0": u = "0": v = "0"
Label1.Caption = a & b & ":" & c & x & ":" & y & z & "." & u & v
End Sub
Public Class StopwatchForm
Private stopwatch As New Stopwatch()
Private elapsedTime As TimeSpan = TimeSpan.Zero
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ResetDisplay()
End Sub
Private Sub clockBtn_Click(sender As Object, e As EventArgs) Handles clockBtn.Click
Timer1.Stop()
displayLabel.Text = DateTime.Now.ToString("HH:mm:ss")
End Sub
Private Sub dateBtn_Click(sender As Object, e As EventArgs) Handles dateBtn.Click
Timer1.Stop()
displayLabel.Text = DateTime.Now.ToString("yyyy-MM-dd")
End Sub
Private Sub stopwatchBtn_Click(sender As Object, e As EventArgs) Handles stopwatchBtn.Click
Timer1.Stop()
ResetDisplay()
End Sub
Private Sub startBtn_Click(sender As Object, e As EventArgs) Handles startBtn.Click
If Not stopwatch.IsRunning Then
stopwatch.Start()
Timer1.Start()
End If
End Sub
Private Sub stopBtn_Click(sender As Object, e As EventArgs) Handles stopBtn.Click
If stopwatch.IsRunning Then
stopwatch.Stop()
elapsedTime = stopwatch.Elapsed
Timer1.Stop()
End If
End Sub
Private Sub resetBtn_Click(sender As Object, e As EventArgs) Handles resetBtn.Click
stopwatch.Reset()
elapsedTime = TimeSpan.Zero
ResetDisplay()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
elapsedTime = stopwatch.Elapsed
displayLabel.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
elapsedTime.Hours, elapsedTime.Minutes,
elapsedTime.Seconds, elapsedTime.Milliseconds \ 10)
End Sub
Private Sub ResetDisplay()
displayLabel.Text = "00:00:00.00"
End Sub
End Class
VB6 timers have limited precision (~55ms), while VB.NET has more accurate timing
VB.NET uses a more modern, object-oriented approach
VB.NET has more consistent syntax and better type handling
VB.NET includes the Stopwatch class for precise timing