|
-
Nov 6th, 2002, 08:45 PM
#1
Thread Starter
Addicted Member
Timer Code
VB Code:
Dim tmrVal As Long
Dim BinVal As String
Private Sub tmrCount_Timer()
BinVal = DecToBin(tmrVal)
For l = -7 To 0
If Mid(BinVal, l + 8, 1) = "1" Then
If shpLED(l + 8).BackColor = &H80& Then
pbxLedDisplay_MouseUp 0, 0, 200 + ((l + 7) * 360), 250
End If
ElseIf Mid(BinVal, l + 8, 1) = "0" Then
If shpLED(l + 8).BackColor = &HFF& Then
pbxLedDisplay_MouseUp 0, 0, 200 + ((l + 7) * 360), 250
End If
End If
Next
If TimerVal = 255 Then
TimerVal = TimerVal - 255
Else
tmrVal = tmrVal + 1
End If
MsgBox tmrVal
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!
-
Nov 6th, 2002, 09:23 PM
#2
I wonder how many charact
Your a victim of brain melting... take a break
VB Code:
If [b]TimerVal[/b] = 255 Then
[b]TimerVal[/b] = TimerVal - 255
Else
[b]tmrVal[/b] = [b]tmrVal[/b] + 1
End If
-
Nov 6th, 2002, 09:27 PM
#3
Thread Starter
Addicted Member
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:
'If TimerVal = 255 Then
' TimerVal = TimerVal - 255
'Else
tmrVal = tmrVal + 1
'End If
Very sorry about that!
-
Nov 6th, 2002, 09:36 PM
#4
I wonder how many charact
You must be setting tmrval = 1 somewhere else in your code...
Hit CTRL-F , enter tmrval , and click Find Next....
-
Nov 6th, 2002, 09:39 PM
#5
Thread Starter
Addicted Member
The only other place is here
VB Code:
Private Sub MDIForm_Load()
ResetSubWindows Me
SetPBarPos Me
tmrVal = 0
End Sub
But thats the form load
-
Nov 6th, 2002, 09:49 PM
#6
I wonder how many charact
Well, if you meant to declare tmrval as global variable, then use
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..
-
Nov 6th, 2002, 10:19 PM
#7
Thread Starter
Addicted Member
Well I don't think I need it public... and it doesn't seem to change the end result anyway
-
Nov 6th, 2002, 11:13 PM
#8
I wonder how many charact
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:
Private Sub Timer1_Timer()
tmrval = tmrval + 1
MsgBox tmrval
End Sub
to read
VB Code:
Private Sub Timer1_Timer()
Dim tmrval as Long 'initializes a long data type, setting it to 0.
tmr=tmrval + 1 'adds 1
msgbox tmrval 'will always be 1
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.
-
Nov 6th, 2002, 11:19 PM
#9
I wonder how many charact
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)
-
Nov 6th, 2002, 11:28 PM
#10
Thread Starter
Addicted Member
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:
Option Explicit
Dim tmrVal As Long
Private Sub MDIForm_Load()
ResetSubWindows Me
SetPBarPos Me
End Sub
Private Sub pbxLedDisplay_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim s As Shape
Dim i As Integer
Set s = ShapeHitTest(X, Y)
If Not s Is Nothing Then
If shpLED(s.Index).BackColor = &H80& Then
shpLED(s.Index).BackColor = &HFF&
lblBinCode = Mid(lblBinCode, 1, (s.Index * 2) - 1) & Replace(lblBinCode, "0", "1", s.Index * 2, 1)
ElseIf shpLED(s.Index).BackColor = &HFF& Then
shpLED(s.Index).BackColor = &H80&
lblBinCode = Mid(lblBinCode, 1, (s.Index * 2) - 1) & Replace(lblBinCode, "1", "0", s.Index * 2, 1)
End If
End If
Dim store As String
For i = 1 To 8
store = store & Mid(lblBinCode, i * 2, 1)
Next
lblBinCode = Mid(lblBinCode, 1, 19) & BinToDec(store)
End Sub
Private Function ShapeHitTest(ByVal X As Single, ByVal Y As Single) As Shape
Dim c As Control
For Each c In Controls
If TypeOf c Is Shape Then
If ((X >= c.Left) And (X <= c.Left + c.Width)) And ((Y >= c.Top) And (Y <= c.Top + c.Height)) Then
Set ShapeHitTest = c
Exit Function
End If
End If
Next
End Function
Private Sub tmrCount_Timer()
Dim l As Integer
For l = -7 To 0
If Mid$(BinToDec(tmrVal), l + 8, 1) = "1" Then 'When I remove the BinToDec() from here...
If shpLED(l + 8).BackColor = &H80& Then
pbxLedDisplay_MouseUp 1, 0, 200 + ((l + 7) * 360), 225
End If
ElseIf Mid$(BinToDec(tmrVal), l + 8, 1) = "0" Then 'and from here, the counter works fine
If shpLED(l + 8).BackColor = &HFF& Then
pbxLedDisplay_MouseUp 1, 0, 200 + ((l + 7) * 360), 225
End If
End If
Next
If tmrVal = 255 Then
tmrVal = tmrVal - 255
Else
tmrVal = tmrVal + 1
End If
MsgBox tmrVal 'This always says 1
End Sub
And this is all in a module
VB Code:
Public Function BaseTwoCheck(DecimalCode As Long) As Long
i = 0
num = 0
Do Until DecimalCode <= num
num = num + 2 ^ i
i = i + 1
Loop
BaseTwoCheck = i - 1
End Function
Public Function DecToBin(DecimalCode As Long) As String
If DecimalCode = 0 Then
DecToBin = "00000000"
Exit Function
End If
For X = BaseTwoCheck(DecimalCode) To 0 Step -1
Y = 2 ^ X
If DecimalCode > (Y - 1) Then
DecToBin = DecToBin & "1"
DecimalCode = DecimalCode - Y
Else
DecToBin = DecToBin & "0"
End If
Next
DecToBin = Format$(DecToBin, "00000000")
End Function
Edit: And I always use Option Explicit, I just didn't add it before
-
Nov 6th, 2002, 11:57 PM
#11
I wonder how many charact
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.
-
Nov 7th, 2002, 10:25 AM
#12
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|