Page 1 of 1

More VB help.

Posted: 2004-04-08 06:36pm
by GoldenFalcon
Alrighty, this time I'm coding a simple stopwatch.

Question 1:

I placed a timer "timStopwatch" in the form "frmStopwatch". Now, the stopwatch code is contained in a module "modStopwatch". All the variables listed have been declared in Sub Main().

Stopwatch code:

Code: Select all

Public Function StopwatchRun()
    If Hours = 99 Then
    frmStopwatch.timStopwatch.Enabled = False
    Message = MsgBox("The timer has reached its limit of 99 hours.  The stopwatch has been shut down.", vbOKOnly + vbExclamation, "Warning")
    Else
        If Minutes = 60 Then
        Hours = Hours + 1
        Minutes = 0
        Else
            If Seconds = 60 Then
            Minutes = Minutes + 1
            Seconds = 0
            Else
                If Milliseconds = 60 Then
                Seconds = Seconds + 1
                Milliseconds = 0
                Else
                Milliseconds = Milliseconds + 1
                End If
            End If
        End If
    End If
    StopwatchRun = Milliseconds
End Function
As you can see it's a whole bunch of if statements which alters the time. The time is shown in a label "lblTime" at the top of the form. And yes I'm aware that only Milliseconds are being shown, haven't changed it to include everything yet. However, even when it shows Seconds it is different as you will see.

Now, for the timer code:

Code: Select all

Private Sub timStopwatch_Timer()
    lblTime.Caption = StopwatchRun()
End Sub
There are buttons in the form which enable/disable timStopwatch. Now here's the problem: I enable timStopwatch and it completely ignores milliseconds above 1. Even when I press my "reset" button when the timer is still on, it resets to zero and returns back to 1, but still sticks to 1. Why's this? :?

Question 2:

How do I make it so lblTime shows:

Hours : Minutes : Seconds : Milliseconds

In that orders? Hours, Minutes, Seconds, and Milliseconds are all variables, but the colons aren't. I thought of:

Code: Select all

lblTime.Caption = Hours, ":", Minutes, ":", Seconds, ":", Milliseconds
But as I first thought it didn't work well. How do I combine variables with normal text?

Thanks if you help...

Posted: 2004-04-08 08:59pm
by Super-Gagme
1 - Your function has no type, try setting it to the type that the variable Milliseconds is returning to it. If that doesn't work, I'll exam in more detail :p

2- To assign variables to a string like the Caption of a label try this.


Code: Select all

lblTime.Caption = Hours & ":" & Minutes & ":" & Seconds & ":" & Milliseconds

Posted: 2004-04-08 11:56pm
by GoldenFalcon
Thanks, it worked. :)