Click to See Complete Forum and Search --> : Timer Events in Class Module


Carl Sorger
October 5th, 2001, 03:22 AM
I am a VB beginner and hope somebody can give me some advice on the following matter:

I am working on a class module that is able to receive the following information:

Controlname: ie. lblInfo
background color of the control: ie. &H00C0FFFF&
foreground color of the control: ie. &H00FF0000&
desired background color: ie. frmData.background
desired foreground color: ie. frmData.background

The class has some internal code that breaks the colornumbers down to rgb colors and subsequently changes the current background and foreground colors of the control to the desired background and foreground colors. In the example above background and foreground color of the lblInfo control would fade to the background color of the frmData, which contains lblInfo.

The code for that is rather simple and works fine. The problem is that I would like to control the speed of the fading process. I don't like the side effects of API Sleep calls or DoWhile Loops with DoEvents. So I wonder if it is possible to use a timer event in a class module?

The following code is in the class module.

Declaration Section:
Private WithEvents Timer1 As betterTimer
...
Private Sub Class_Initialize()
Set Timer1 = New betterTimer
Timer1.Interval = 100
Timer1.Enabled = True
End Sub
...
Public Sub Timer1_Timer
code for fading ...
End Sub

So it is possible to get a Sub Timer1_Timer into my class module, but if I call the sub from my main form:

Dim calc As New clsFade
calc.Timer1_Timer

then the programm runs through the Sub Timer1_Timer once and returns to the calling form. I guess that is to be expected. But what can I do to make the programm go over the code in the Timer routine at every timer interval until I set timer.enabled = False? Is it possible at all to do that in a class module?

Your help would be highly appreciated. Thank you.

Carl

Cakkie
October 5th, 2001, 03:45 AM
Well, it is the betterTimer control/class that needs to do that. I don't know that control, but I can imaging that the control should raise the Timer event every time the interval passed. If you have written the bettertimer yourself, you might want to share the code with us, cause my guess is that the problem will reside there.

Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook

Cimperiali
October 5th, 2001, 04:13 AM
SetTimer and KillTimer Api may help.
Download Api-guide (a free tool), and search for those api there
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net


Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

Clearcode
October 5th, 2001, 04:50 AM
A timer (VB intrinsic or otherwise) requires a form to which it sends its 'clicks' (actually it sends WM_TIMER messages) so your betterTimer class should have a property like .hwnd that should be set.

HTH,
Duncan

-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.

Cimperiali
October 5th, 2001, 05:09 AM
;-)

Have a nice day, you Guru.

;-)



Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

Chris Eastwood
October 5th, 2001, 05:58 AM
<delurk>

There used to be a sample that came with VB / MSDN disks ages ago that showed how to do this with the 'SetTimer' api calls. Can't find it now I'm afraid.

I did a search on CodeHound and found a nearly identical sample though :

http://vb.oreilly.com/news/vb_tips_1098.html#9(Tip #9)

- it looks pretty identical to the M$ version of it.

</delurk>



Chris Eastwood
VBCodeLibrary - http://www.vbcodelibrary.com

Cakkie
October 5th, 2001, 06:08 AM
Okay Chris, what's with all the delurking here, this is a decent forum :wink (hmm, I withdraw my words...)

Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook

Chris Eastwood
October 5th, 2001, 06:43 AM
Nothing to see here, move along.

:smoke

Chris Eastwood
VBCodeLibrary - http://www.vbcodelibrary.com

Cimperiali
October 5th, 2001, 07:02 AM
>The server encountered an error and cannot complete your request. The error reported was:
>You have already reached the limit of 5 today

You're right, Chris: no decent forum...

(To everybody: just kidding! I like this forum!!)




Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

DSJ
October 5th, 2001, 08:14 AM
Here's the code for a timer class... I "stold" most of this from someplace, don't remember where. There are some things that probably need to be tweeked, but this should get you started...


'in class module...
option Explicit

public Enum tmrUnit
tmrmilliseconds = 1
tmrSeconds = 2
tmrMinutes = 3
tmrHours = 4
tmrDays = 5
tmrMonths = 6
End Enum

public Enum tmrRecurrence
tmrOnce = 1
tmrContinuous = 2
End Enum

private mlngInterval as Long
private mintTimeunit as tmrUnit
private gintRecurrence as Integer
private glngTimerId as Long
public Event TimerFired()

public property get Interval() as Long
Interval = mlngInterval
End property

public property let Interval(byval lngNewValue as Long)
mlngInterval = lngNewValue
End property

public property get TimeUnit() as tmrUnit
TimeUnit = mintTimeunit
End property

public property let TimeUnit(byval intUnit as tmrUnit)
mintTimeunit = intUnit
End property

public property get Recurrence() as tmrRecurrence
Recurrence = gintRecurrence
End property

public property let Recurrence(byval intNewValue as tmrRecurrence)
gintRecurrence = intNewValue
End property

public property get TimerId() as Long
TimerId = glngTimerId
End property

public Sub InitializeTimer(optional lngInterval as Long, optional intTimerUnit as tmrUnit, optional intRecurrence as tmrRecurrence)
Dim lngTimerId as Long
Dim lngIntervalInMS as Long

If glngTimerId <> 0 then Exit Sub

If lngInterval <> 0 then Interval = lngInterval
If intTimerUnit <> 0 then TimeUnit = intTimerUnit
If intRecurrence <> 0 then Recurrence = intRecurrence

Select Case TimeUnit
Case tmrmilliseconds
lngIntervalInMS = Interval
Case tmrSeconds
lngIntervalInMS = Interval * 1000
Case tmrMinutes
lngIntervalInMS = Interval * 60000
Case tmrHours
lngIntervalInMS = Interval * 3600000
Case tmrDays
lngIntervalInMS = Interval * 86400000
Case tmrMonths
lngIntervalInMS = Interval * 2592000000#
End Select

lngTimerId = SetTimer(0, 0, lngIntervalInMS, AddressOf TimerProc)
If lngTimerId = 0 then
Err.Raise vbObjectError + 101, "Timer", "set Timer failed"
else
glngTimerId = lngTimerId
End If
End Sub

public Sub DestroyTimer()
Dim lngReturn as Long

If glngTimerId <> 0 then
lngReturn = KillTimer(0, glngTimerId)
If lngReturn = 0 then
Err.Raise vbObjectError + 102, "Timer", "KillTimer Faile."
else
glngTimerId = 0
End If
End If
End Sub

friend Sub FireTimerFired()
RaiseEvent TimerFired
End Sub

private Sub Class_Initialize()
mlngInterval = 5
mintTimeunit = tmrSeconds
gintRecurrence = tmrContinuous
glngTimerId = 0

set gobjTimerObj = me
End Sub

private Sub Class_Terminate()
DestroyTimer
End Sub

'in regular module....
option Explicit
public Declare Function SetTimer Lib "user32" (byval hwnd as Long, byval nIDEvent as Long, byval uElapse as Long, byval lpTimerFunc as Long) as Long
public Declare Function KillTimer Lib "user32" (byval hwnd as Long, byval nIDEvent as Long) as Long

public glngTimerId as Long
public gintRecurrence as tmrRecurrence
public gobjTimerObj as MHATimer

public Sub TimerProc(byval hwnd as Long, byval uMsg as Long, byval idEvent as Long, byval dwTimer as Long)

'YOUR CODE HERE......

If gintRecurrence = tmrOnce then
gobjTimerObj.DestroyTimer
End If

End Sub

Clearcode
October 5th, 2001, 08:41 AM
You missed a bit.....


public Sub TimerProc(byval hwnd as Long, byval uMsg as Long, byval idEvent as Long, byval dwTimer as Long)

'\\ Trigger the timer event.....
Call FireTimerFired

If gintRecurrence = tmrOnce then
gobjTimerObj.DestroyTimer
End If
End Sub





HTH,
Duncan

-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.

DSJ
October 5th, 2001, 08:46 AM
Thanks for the catch... it's been a while since I was playing around with it and guess I left it broken.

Carl Sorger
October 6th, 2001, 02:26 PM
I like this forum ...
Thanks to all of you for your help and interest. I guess you provided the hints I needed to make my application work. Have a nice weekend...

Carl