Results 1 to 10 of 10

Thread: Array Problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Array Problem

    Hi all,
    I am using arrays for a timer in one of the programs I am creating. I have a stop watch that starts counting when the timer is enabled.

    I have three arrays:

    lblTime(2) - for milliseconds
    lblTime(1) - for seconds
    lblTime(3) - for minutes

    The problem is that when it comes to the part when the minutes have to be changed i.e. when 60 seconds occur... I get an error saying that lblTime(3) Array does not exist.

    Here is the code I am using:

    VB Code:
    1. Private Sub Timer_Timer()
    2.  
    3.     If Me.Enabled = True Then
    4.    
    5.         T2 = T2 + 1        
    6.         If T2 < 10 Then        
    7.             lblTime(2).Caption = "0" & T2            
    8.         Else        
    9.             lblTime(2).Caption = T2            
    10.         End If
    11.        
    12.         If T2 = 60 Then        
    13.             T2 = 0
    14.             T1 = T1 + 1            
    15.             If T1 < 10 Then            
    16.                 lblTime(1).Caption = "0" & T1            
    17.             Else            
    18.                 lblTime(1).Caption = T1            
    19.             End If            
    20.         End If
    21.        
    22.         If T1 = 60 Then        
    23.             T1 = 0
    24.             T0 = T0 + 1            
    25.             If T0 < 10 Then          
    26.                 lblTime(3).Caption = "0" & T0                
    27.             Else            
    28.                 lblTime(3).Caption = T0              
    29.             End If            
    30.         End If      
    31.     End If
    32.    
    33. End Sub

    Any help would be appreciated...

    Khanjan
    Hey... If you found this post helpful please rate it.

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Array Problem

    Timers shouldn't be used to keep time - they're really inaccurate, I would suggest doing something like this:
    VB Code:
    1. Dim dblTime As Double
    2.  
    3. Private Sub Command1_Click()
    4.     dblTime = Timer
    5.     Timer1.Enabled = True
    6. End Sub
    7.  
    8. Private Sub Timer1_Timer()
    9.     Dim dblDiff As Double
    10.     dblDiff = Timer - dblTime
    11.     lblTime(1).Caption = Format$(dblDiff \ 60, "00")
    12.     lblTime(2).Caption = Format$(dblDiff Mod 60, "00")
    13.     lblTime(3).Caption = Format$(dblDiff - Int(dblDiff), "000")
    14. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Array Problem

    Well I can trust that the code works... But could you explain it.

    I really don't want to use codes that I don't understand... I would really appreciate you explaining it.

    Khanjan
    Hey... If you found this post helpful please rate it.

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Array Problem

    VB Code:
    1. Dim dblTime As Double
    2.  
    3. Private Sub Command1_Click()
    4.     ' the Timer function returns the number of seconds since midnight
    5.     ' the decimal part is number of milliseconds.
    6.     ' here we are storing the moment we start Timer1 in dblTime
    7.     dblTime = Timer
    8.     Timer1.Enabled = True
    9. End Sub
    10.  
    11. Private Sub Timer1_Timer()
    12.     Dim dblDiff As Double
    13.     ' We work out the difference between now and the time Timer1 started
    14.     ' since the Timer function wraps round to 0 at Midnight, we check if that
    15.     ' has occured and if it has we add 86400 (24 hours) - i forgot this above
    16.     lDiff = Timer - lTime + IIf(Timer < lTime, 86400, 0)
    17.  
    18.     ' integer division by 60 gives us the number of whole times 60 goes into the
    19.     ' difference - hence that is seconds
    20.     lblTime(1).Caption = Format$(dblDiff \ 60, "00")
    21.     ' Mod 60 gives us the remainder and hence the number of seconds
    22.     lblTime(2).Caption = Format$(dblDiff Mod 60, "00")
    23.     ' The fractional part of the difference represents the number of milliseconds
    24.     lblTime(3).Caption = Format$(dblDiff - Int(dblDiff), "000")
    25. End Sub

  5. #5

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Array Problem

    Is that an API? If you could provide an example... I would appreciate it.

    Thanx

    Khanjan
    Hey... If you found this post helpful please rate it.

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Array Problem

    From the API Guide.
    VB Code:
    1. 'Example by Alexey ([email protected])
    2. 'This example requires one command button (Command1)
    3.  
    4. Option Explicit
    5. Private Declare Function timeGetTime Lib "winmm.dll" () As Long
    6. Private Declare Function SetTextCharacterExtra Lib "gdi32" _
    7. (ByVal hdc As Long, ByVal nCharExtra As Long) As Long
    8.  
    9. Private Type RECT
    10. Left As Long
    11. Top As Long
    12. Right As Long
    13. Bottom As Long
    14. End Type
    15.  
    16. Private Declare Function OffsetRect Lib "user32" (lpRect _
    17. As RECT, ByVal x As Long, ByVal y As Long) As Long
    18.  
    19. Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc _
    20. As Long, ByVal crColor As Long) As Long
    21.  
    22. Private Declare Function FillRect Lib "user32" (ByVal hdc As _
    23. Long, lpRect As RECT, ByVal hBrush As Long) As Long
    24.  
    25. Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal _
    26. crColor As Long) As Long
    27.  
    28. Private Declare Function DeleteObject Lib "gdi32" (ByVal _
    29. hObject As Long) As Long
    30.  
    31. Private Declare Function GetSysColor Lib "user32" (ByVal _
    32. nIndex As Long) As Long
    33.  
    34. Private Const COLOR_BTNFACE = 15
    35.  
    36. Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" _
    37. (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal _
    38. lpString As String, ByVal nCount As Long) As Long
    39.  
    40. Private Declare Function DrawText Lib "user32" Alias "DrawTextA" _
    41. (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, _
    42. lpRect As RECT, ByVal wFormat As Long) As Long
    43.  
    44. Private Const DT_BOTTOM = &H8
    45. Private Const DT_CALCRECT = &H400
    46. Private Const DT_CENTER = &H1
    47. Private Const DT_CHARSTREAM = 4 ' Character-stream, PLP
    48. Private Const DT_DISPFILE = 6 ' Display-file
    49. Private Const DT_EXPANDTABS = &H40
    50. Private Const DT_EXTERNALLEADING = &H200
    51. Private Const DT_INTERNAL = &H1000
    52. Private Const DT_LEFT = &H0
    53. Private Const DT_METAFILE = 5 ' Metafile, VDM
    54. Private Const DT_NOCLIP = &H100
    55. Private Const DT_NOPREFIX = &H800
    56. Private Const DT_PLOTTER = 0 ' Vector plotter
    57. Private Const DT_RASCAMERA = 3 ' Raster camera
    58. Private Const DT_RASDISPLAY = 1 ' Raster display
    59. Private Const DT_RASPRINTER = 2 ' Raster printer
    60. Private Const DT_RIGHT = &H2
    61. Private Const DT_SINGLELINE = &H20
    62. Private Const DT_TABSTOP = &H80
    63. Private Const DT_TOP = &H0
    64. Private Const DT_VCENTER = &H4
    65. Private Const DT_WORDBREAK = &H10
    66.  
    67. Private Declare Function OleTranslateColor Lib "olepro32.dll" _
    68. (ByVal OLE_COLOR As Long, ByVal hPalette As Long, pccolorref As Long) As Long
    69. Private Const CLR_INVALID = -1
    70.  
    71. Public Sub TextEffect(obj As Object, ByVal sText As String, _
    72. ByVal lX As Long, ByVal lY As Long, Optional ByVal bLoop _
    73. As Boolean = False, Optional ByVal lStartSpacing As Long = 128, _
    74. Optional ByVal lEndSpacing As Long = -1, Optional ByVal oColor _
    75. As OLE_COLOR = vbWindowText)
    76.  
    77. Dim lhDC As Long
    78. Dim i As Long
    79. Dim x As Long
    80. Dim lLen As Long
    81. Dim hBrush As Long
    82. Static tR As RECT
    83. Dim iDir As Long
    84. Dim bNotFirstTime As Boolean
    85. Dim lTime As Long
    86. Dim lIter As Long
    87. Dim bSlowDown As Boolean
    88. Dim lCOlor As Long
    89. Dim bDoIt As Boolean
    90.  
    91. lhDC = obj.hdc
    92. iDir = -1
    93. i = lStartSpacing
    94. tR.Left = lX: tR.Top = lY: tR.Right = lX: tR.Bottom = lY
    95. OleTranslateColor oColor, 0, lCOlor
    96.  
    97. hBrush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE))
    98. lLen = Len(sText)
    99.  
    100. SetTextColor lhDC, lCOlor
    101. bDoIt = True
    102.  
    103. Do While bDoIt
    104. lTime = timeGetTime
    105. If (i < -3) And Not (bLoop) And Not (bSlowDown) Then
    106. bSlowDown = True
    107. iDir = 1
    108. lIter = (i + 4)
    109. End If
    110. If (i > 128) Then iDir = -1
    111. If Not (bLoop) And iDir = 1 Then
    112. If (i = lEndSpacing) Then
    113. ' Stop
    114. bDoIt = False
    115. Else
    116. lIter = lIter - 1
    117. If (lIter <= 0) Then
    118. i = i + iDir
    119. lIter = (i + 4)
    120. End If
    121. End If
    122. Else
    123. i = i + iDir
    124. End If
    125.  
    126. FillRect lhDC, tR, hBrush
    127. x = 32 - (i * lLen)
    128. SetTextCharacterExtra lhDC, i
    129. DrawText lhDC, sText, lLen, tR, DT_CALCRECT
    130. tR.Right = tR.Right + 4
    131. If (tR.Right > obj.ScaleWidth \ Screen.TwipsPerPixelX) Then _
    132. tR.Right = obj.ScaleWidth \ Screen.TwipsPerPixelX
    133. DrawText lhDC, sText, lLen, tR, DT_LEFT
    134. obj.Refresh
    135.  
    136. Do
    137. DoEvents
    138. If obj.Visible = False Then Exit Sub
    139. Loop While (timeGetTime - lTime) < 20
    140.  
    141. Loop
    142. DeleteObject hBrush
    143.  
    144. End Sub
    145.  
    146. Private Sub Command1_Click()
    147. Me.ScaleMode = vbTwips
    148. Me.AutoRedraw = True
    149. Call TextEffect(Me, "Hello World!", 12, 12, False, 128)
    150. End Sub

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Array Problem

    well if you're looking for millisecond accuracy then use the QueryPerformance APIs.

    Using the code in post #4 the displayed time will always be +/- 40 milliseconds (or whatever it's accuracy is). Using a timer in the manner you have in post #1 would loose milliseconds every time it fired.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2005
    Location
    Toronto, Canada
    Posts
    357

    Re: Array Problem

    So do the Query Performance APIs provide exact millisecond accuracy. If not, do they provide better accuracy than the timegetTime API in post 7

    Khanjan
    Hey... If you found this post helpful please rate it.

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Array Problem

    I believe QueryPerformance APIs provide the highest accuracy you can get through standard means.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width