Results 1 to 2 of 2

Thread: timer control help

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi, I'm having a tough time here with the timer control. To illustrate the problem I'm having, I created a simple application. The application has a timer control, progress bar and 2 buttons.

    What I'm trying to accomplish is that when I hit the Stop button, I want the timer to stop/reset and have the progress bar go back to starting position. Then, when I het the Start button, I want it to start over. Everything works fine EXCEPT that I can't get the Timer to reset. If I hit Stop and then Start, the time starts where it left of thus causing the progress bar to jump to the position it left off at when the Stop button was hit. However, if I let the progress bar continue until Max, it works fine.. It's only when I Stop it in the middle..

    The problem seems to be that I can't manipulate the Static value from outside the Timer control. So that's why it is storing the last value and then continuing from that value.

    I have included the code below so that you can test it yourself and hopefully come up with a solution for me.

    Any and all help would be appreciated..

    Code:
    Private Sub Form_Load()
    
    Timer1.Interval = 125
    Timer1.Enabled = False
    prgBar1.Max = 20
    
    End Sub
    
    Private Sub cmdStart_Click()
    Timer1.Enabled = True
    End Sub
    
    Private Sub cmdStop_Click()
    Timer1.Enabled = False
    prgBar1.Value = prgBar1.Min
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    Timer1.Enabled = False
    End Sub
    
    Private Sub Timer1_Timer()
    Static intTime
    If IsEmpty(intTime) Then intTime = 0.125
    
    prgBar1.Value = intTime
    
    If intTime = prgBar1.Max Then
        Timer1.Enabled = False
        intTime = 0.125
        prgBar1.Value = prgBar1.Min
    Else
        intTime = intTime + 0.125
    End If
    End Sub

  2. #2
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    Smile

    Simple mistake. Don't use the Static var in the timer control, instead Dim the var "intTime" in declarations section. Then add intTime = 0 to the stop button. The Static var can not be accessed by other subs. I tried your code and then made these changes and it worked fine. Let me know if you need anything else.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

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