CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2001
    Posts
    2

    Timer Events in Class Module

    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




  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Timer Events in Class Module

    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
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Timer Events in Class Module

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Timer Events in Class Module

    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.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Luckily you're back!

    ;-)

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Timer Events in Class Module

    &lt;delurk&gt;

    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.

    &lt;/delurk&gt;



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

  7. #7
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Timer Events in Class Module

    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
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  8. #8
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Timer Events in Class Module

    Nothing to see here, move along.

    :smoke

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

  9. #9
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Timer Events in Class Module

    &gt;The server encountered an error and cannot complete your request. The error reported was:
    &gt;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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  10. #10
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Timer Events in Class Module

    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 &lt;&gt; 0 then Exit Sub

    If lngInterval &lt;&gt; 0 then Interval = lngInterval
    If intTimerUnit &lt;&gt; 0 then TimeUnit = intTimerUnit
    If intRecurrence &lt;&gt; 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 &lt;&gt; 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






  11. #11
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Timer Events in Class Module

    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.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  12. #12
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Timer Events in Class Module

    Thanks for the catch... it's been a while since I was playing around with it and guess I left it broken.


  13. #13
    Join Date
    Oct 2001
    Posts
    2

    Re: Timer Events in Class Module

    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


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

Featured