Results 1 to 12 of 12

Thread: Timer Code

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134

    Timer Code

    VB Code:
    1. Dim tmrVal As Long
    2. Dim BinVal As String
    3.  
    4. Private Sub tmrCount_Timer()
    5.     BinVal = DecToBin(tmrVal)
    6.     For l = -7 To 0
    7.         If Mid(BinVal, l + 8, 1) = "1" Then
    8.             If shpLED(l + 8).BackColor = &H80& Then
    9.                 pbxLedDisplay_MouseUp 0, 0, 200 + ((l + 7) * 360), 250
    10.             End If
    11.         ElseIf Mid(BinVal, l + 8, 1) = "0" Then
    12.             If shpLED(l + 8).BackColor = &HFF& Then
    13.                 pbxLedDisplay_MouseUp 0, 0, 200 + ((l + 7) * 360), 250
    14.             End If
    15.         End If
    16.     Next
    17.     If TimerVal = 255 Then
    18.         TimerVal = TimerVal - 255
    19.     Else
    20.         tmrVal = tmrVal + 1
    21.     End If
    22.     MsgBox tmrVal
    23. End Sub
    Anyone have any ideas why at the end of every timer interval (100ms) tmrVal always equals 1? this sub is the only place tmrVal is being used!

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Your a victim of brain melting... take a break
    VB Code:
    1. If [b]TimerVal[/b] = 255 Then
    2.         [b]TimerVal[/b] = TimerVal - 255
    3.     Else
    4.         [b]tmrVal[/b] = [b]tmrVal[/b] + 1
    5.     End If

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134
    Oh no, sorry, the reason was that I WAS using that, but I had already commented it out, I only took out the single quotes because it was what I originally had...

    RIGHT now my code looks like this:
    VB Code:
    1. 'If TimerVal = 255 Then
    2.     '    TimerVal = TimerVal - 255
    3.     'Else
    4.         tmrVal = tmrVal + 1
    5.     'End If
    Very sorry about that!

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You must be setting tmrval = 1 somewhere else in your code...

    Hit CTRL-F , enter tmrval , and click Find Next....

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134
    The only other place is here
    VB Code:
    1. Private Sub MDIForm_Load()
    2.     ResetSubWindows Me
    3.     SetPBarPos Me
    4.     tmrVal = 0
    5. End Sub

    But thats the form load

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, if you meant to declare tmrval as global variable, then use
    VB Code:
    1. Public tmrVal As Long
    in your parent form....

    I imagine if you have OPTION EXPLICIT at top of all your forms... the IDE would tell you the variable was not declared..

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134
    Well I don't think I need it public... and it doesn't seem to change the end result anyway

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    The problem is where your declaring tmrVal....

    If you declare in the parent form as so: Dim tmrVal As Long , then incrementing tmrval in your child form won't affect that value... since its only available to the parent form, not the child form (MDIForm).... if you need to use the tmrVal in the parent form, declare it as Public...

    You don't get an error when you use the code in your MDIFORM because the compiler changes this code:
    VB Code:
    1. Private Sub Timer1_Timer()
    2. tmrval = tmrval + 1
    3. MsgBox tmrval
    4. End Sub

    to read
    VB Code:
    1. Private Sub Timer1_Timer()
    2. Dim tmrval as Long  'initializes a long data type, setting it to 0.
    3. tmr=tmrval + 1        'adds 1
    4. msgbox tmrval        'will always be 1
    5. End Sub

    You see... you don't have Option Explicit, so the compiler is letting you get away with not declaring a variable, so it creates one everytime the _Timer Event is called... and it initializes it to zero, and you add 1 to it... so its always one.

    To get around this... since you don't need it global... declare Dim tmrVal As Long at the top of your MDIFORM... then the compiler knows that this variable will persist and keep its value...
    Last edited by nemaroller; Nov 6th, 2002 at 11:17 PM.

  9. #9
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Use OPTION EXPLICIT at the top of all your forms... and it will become clear why it worked... (or go to TOOLS...EDITOR... REQUIRE VARIABLE DECLARATION)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134
    Ok, well, all of this is being done in the MDIForm, Here, I'll post all relevant code...


    This is all in my MDIForm
    VB Code:
    1. Option Explicit
    2. Dim tmrVal As Long
    3.  
    4. Private Sub MDIForm_Load()
    5.     ResetSubWindows Me
    6.     SetPBarPos Me
    7. End Sub
    8.  
    9. Private Sub pbxLedDisplay_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    10.     Dim s As Shape
    11.     Dim i As Integer
    12.     Set s = ShapeHitTest(X, Y)
    13.     If Not s Is Nothing Then
    14.         If shpLED(s.Index).BackColor = &H80& Then
    15.             shpLED(s.Index).BackColor = &HFF&
    16.             lblBinCode = Mid(lblBinCode, 1, (s.Index * 2) - 1) & Replace(lblBinCode, "0", "1", s.Index * 2, 1)
    17.         ElseIf shpLED(s.Index).BackColor = &HFF& Then
    18.             shpLED(s.Index).BackColor = &H80&
    19.             lblBinCode = Mid(lblBinCode, 1, (s.Index * 2) - 1) & Replace(lblBinCode, "1", "0", s.Index * 2, 1)
    20.         End If
    21.     End If
    22.     Dim store As String
    23.     For i = 1 To 8
    24.         store = store & Mid(lblBinCode, i * 2, 1)
    25.     Next
    26.     lblBinCode = Mid(lblBinCode, 1, 19) & BinToDec(store)
    27. End Sub
    28.  
    29. Private Function ShapeHitTest(ByVal X As Single, ByVal Y As Single) As Shape
    30.     Dim c As Control
    31.     For Each c In Controls
    32.         If TypeOf c Is Shape Then
    33.             If ((X >= c.Left) And (X <= c.Left + c.Width)) And ((Y >= c.Top) And (Y <= c.Top + c.Height)) Then
    34.                 Set ShapeHitTest = c
    35.                 Exit Function
    36.             End If
    37.         End If
    38.     Next
    39. End Function
    40.  
    41. Private Sub tmrCount_Timer()
    42.     Dim l As Integer
    43.     For l = -7 To 0
    44.         If Mid$(BinToDec(tmrVal), l + 8, 1) = "1" Then 'When I remove the BinToDec() from here...
    45.             If shpLED(l + 8).BackColor = &H80& Then
    46.                 pbxLedDisplay_MouseUp 1, 0, 200 + ((l + 7) * 360), 225
    47.             End If
    48.         ElseIf Mid$(BinToDec(tmrVal), l + 8, 1) = "0" Then 'and from here, the counter works fine
    49.             If shpLED(l + 8).BackColor = &HFF& Then
    50.                 pbxLedDisplay_MouseUp 1, 0, 200 + ((l + 7) * 360), 225
    51.             End If
    52.         End If
    53.     Next
    54.     If tmrVal = 255 Then
    55.         tmrVal = tmrVal - 255
    56.     Else
    57.         tmrVal = tmrVal + 1
    58.     End If
    59.     MsgBox tmrVal 'This always says 1
    60. End Sub

    And this is all in a module
    VB Code:
    1. Public Function BaseTwoCheck(DecimalCode As Long) As Long
    2.     i = 0
    3.     num = 0
    4.     Do Until DecimalCode <= num
    5.         num = num + 2 ^ i
    6.         i = i + 1
    7.     Loop
    8.     BaseTwoCheck = i - 1
    9. End Function
    10.  
    11. Public Function DecToBin(DecimalCode As Long) As String
    12.     If DecimalCode = 0 Then
    13.         DecToBin = "00000000"
    14.         Exit Function
    15.     End If
    16.     For X = BaseTwoCheck(DecimalCode) To 0 Step -1
    17.         Y = 2 ^ X
    18.         If DecimalCode > (Y - 1) Then
    19.             DecToBin = DecToBin & "1"
    20.             DecimalCode = DecimalCode - Y
    21.         Else
    22.             DecToBin = DecToBin & "0"
    23.         End If
    24.     Next
    25.     DecToBin = Format$(DecToBin, "00000000")
    26. End Function

    Edit: And I always use Option Explicit, I just didn't add it before

  11. #11
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Sorry, from initial impressions, it looked like you were simply just declaring tmrval in the wrong form. I must admit I'm stumped....
    as long as you did Find Next on tmrval for the entire project, I can't understand why calling a module level function would reset tmrval to zero... when I simulated the same setup...a form that loads a MDIFORM with a timer, and calls the Dec2Bin function in a module..... the counter did not reset... so its something in your app...
    Last edited by nemaroller; Nov 7th, 2002 at 12:05 AM.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134
    Don't ask me how, but I fixed it... (rewrote nearly all of it) But thanks for all your help
    Last edited by Xerpher; Nov 7th, 2002 at 10:33 AM.

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