[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Change Colors, along with start a second timer

Posted on 2005-04-09
3
Medium Priority
?
223 Views
Last Modified: 2010-05-02
I have this script below, and I actually have 2 questions about it. First.... Where is says "HERE" is clicked how do i get another timer going to change the EndTime accordenly dpending on how long the Timer (main one) is "Paused"... Also I can NOT figure out how to change the font color when there is 10 min left, and 3 min left.... I tried: Display.ForeGround = vbRed In a IF THEN statement, but i doesn seem to work....

Dim ClockRunning As Byte
Dim PauseState As Byte
Dim ViewOptions As Byte
Dim Counter As Long
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
For i = 0 To Forms.Count - 1
     Unload Forms(i)
Next i
End
End Sub

Private Sub Options_Click()
    If ViewOptions = Flase Then
        RoundClock.Height = 6810
        RoundClock.Width = 8115
        ViewOptions = True
    Else
        RoundClock.Height = 3330
        RoundClock.Width = 3360
        ViewOptions = False
    End If
End Sub
Private Sub Set40_Click()
    MinSet.Text = "40"
End Sub
Private Sub Set45_Click()
    MinSet.Text = "45"
End Sub
Private Sub Set50_Click()
    MinSet.Text = "50"
End Sub
Private Sub Start_Click()
    If ClockRunning = False Then
        If PauseState = False Then
            Counter = Val(MinSet.Text) * 60
            Timer.Interval = 1000
            Timer.Enabled = True
            StartTime.Text = DatePart("h", Time) & ":" & DatePart("n", Time) & ":" & DatePart("s", Time)
            EndTime.Text = DatePart("h", DateAdd("n", Val(MinSet.Text), Time)) & ":" & DatePart("n", DateAdd("n", Val(MinSet.Text), Time)) & ":" & DatePart("s", DateAdd("n", Val(MinSet.Text), Time))
            ClockIMG.Visible = True
            Display.Show
            Start.Caption = "Pause"
            ClockRunning = True
            MinSet.Enabled = False
            MinSet.Locked = True
        Else
            Timer.Enabled = True
            PauseState = False
            ClockRunning = True
            Start.Caption = "Pause"
        End If
    Else
       "<<<<<<<<<<<HERE>>>>>>>>>>>>>>>"
        Start.Caption = "Resume"
        Timer.Enabled = False
        ClockRunning = False
        PauseState = True
    End If
       
End Sub
Private Sub Stop_Click()
    StartTime.Text = ""
    EndTime.Text = ""
    MinSet.Text = ""
    MinLeft.Text = ""
    Timer.Enabled = False
    ClockIMG.Visible = False
    ClockRunning = False
    PauseState = False
    Start.Caption = "Start"
    Display.Cls
    MinSet.Enabled = True
    MinSet.Locked = Flase
End Sub
Private Sub Timer_Timer()
Dim Msg As String
Dim MsgLeft As String
    Msg = Counter \ 60 & ":" & Counter Mod 60
    MsgLeft = Counter \ 60
    If Not Display.Visible Or Counter <= 0 Then
        Display.CurrentX = (Display.ScaleWidth - Display.TextWidth(Msg)) / 2
        Display.CurrentY = (Display.ScaleHeight - Display.TextHeight(Msg)) / 2
        Display.Print "Times Up"
        Timer.Enabled = False
        ClockIMG.Visible = False
        ClockRunning = False
        PauseState = False
        Display.ForeColor = vbWhite
        Exit Sub
    End If
    Display.Cls
    Display.CurrentX = (Display.ScaleWidth - Display.TextWidth(Msg)) / 2
    Display.CurrentY = (Display.ScaleHeight - Display.TextHeight(Msg)) / 2
    Display.Print Msg
    MinLeft.Text = MsgLeft
    Counter = Counter - 1
End Sub
0
Comment
Question by:Gunit2507
  • 3
3 Comments
 
LVL 6

Expert Comment

by:GPrentice00
ID: 13803486

First, let me recommend that you use Option Explicit in each of your forms and modules, so that you explicitly define each variable and object in use – you’re already doing that, that’s good, but the Option Explicit also goes a step beyond forcing you to specify the variables, it helps with simplistic argument assignments as well.  The reason I suggest, is that you have some typos in your property assignments:
....
  If ViewOptions = Flase Then        
....
  MinSet.Locked = Flase


Without option explicit, flase is just a variable with no value; with it, the compiler detects that you don’t have a variable called flase, and errors.  While in this program, it causes no problems because "flase" and "false" both have the same value (zero), the argument can not be said for mistyping ‘true’ for object.property = ture….
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 13803495

It took a bit of effort to reconstruct a working project from your code, and not all items there are clear, such as the “Options” and “RoundClock” things, but they don’t appear to have any influence on the functionality questions you have, and were not necessary to build a working project shell...

While this code is a very odd way of pulling off the job, it does work thus I shall just modify the core of it, preserving the code structure you have as much as possible rather than offer 300 different ways to do the same job - aiming to give you something to reverse-engineer for your own purposes later primarily.

First, the timer event needs a major overhauling (mostly in rearrangment, thus easier to repost in entirety) in order to comply with your wishes, and in here, you can see how commands are moved around.  I’ve also shown how and where to put in the conditions to change your text color depending on time remaining, and ‘prettied’ the time display message from 3:9 to 3:09 etc.

As for the second part of the problem – another timer part where you say “===here===” will be in my next comment.



Private Sub Timer_Timer()
    Dim Msg As String
    Dim MsgLeft As String
    Msg = Counter \ 60 & ":" & Format$(Counter Mod 60, "00")   'to show :09 instead of :9
    MsgLeft = Counter \ 60
    If Not Display.Visible Or Counter <= 0 Then
        Display.Cls      'must clear out the 0:01 counter display here
        Display.ForeColor = vbWhite  'must set the color before printing output
        ' make textposition based on the "times up" message that WILL be put there)
        Display.CurrentX = (Display.ScaleWidth - Display.TextWidth("Times Up")) / 2
        Display.CurrentY = (Display.ScaleHeight - Display.TextHeight("Times Up")) / 2
        Display.Print "Times Up"
        Timer.Enabled = False
        clockimg.Visible = False
        ClockRunning = False
        PauseState = False        
        Exit Sub
    End If
    Display.Cls
    ' change the color for different times by setting the conditions here... This does red text for under 3 minutes, and yellow for equal to and less than 1 minute
    If Counter < (3 * 60) Then
        Display.ForeColor = vbRed
        If Counter <= (1 * 60) Then
            Display.ForeColor = vbYellow
        End If
    End If
    Display.CurrentX = (Display.ScaleWidth - Display.TextWidth(Msg)) / 2
    Display.CurrentY = (Display.ScaleHeight - Display.TextHeight(Msg)) / 2
    Display.Print Msg
    minLeft.Text = MsgLeft    
    Counter = Counter - 1
End Sub
0
 
LVL 6

Accepted Solution

by:
GPrentice00 earned 2000 total points
ID: 13803501

To simplify your string construction (and use less computations as well), use the string formatting features and avoid breaking up the times into parts, reducing the two strings here:
            startTime.Text = DatePart("h", Time) & ":" & DatePart("n", Time) & ":" & DatePart("s", Time)
            endtime.Text = DatePart("h", DateAdd("n", Val(MinSet.Text), Time)) & ":" & DatePart("n", DateAdd("n", Val(MinSet.Text), Time)) & ":" & DatePart("s", DateAdd("n", Val(MinSet.Text), Time))

to much simplier
           startTime.Text = Format$(Time, "hh:mm:ss")            
            endtime.Text = Format$(DateAdd("n", Val(MinSet.Text), Time), "hh:mm:ss")

the dateadd function with adding minutes or seconds will also bump the time to the correct 24h display going past midnight….

============================

With that change in mind, add a second timer to the form, called TimerDuringPause, with interval of 1000 and disabled at design time.

Here is the code for its firing event:
Private Sub TimerDuringPause_Timer()
‘   Increase the displayed end-time time by 1 second and redisplay in time format
‘  This gets and redisplays directly from the form display, so it could be improved upon
    Dim TimeEndedNew As Date
    TimeEndedNew = DateAdd("s", 1, CDate(endtime.Text))
    endtime.Text = Format(TimeEndedNew, "hh:mm:ss")
End Sub

Now, modifying your start/pause/resume button code to reflect these changes:
Private Sub Start_Click()
' clockrunning indicates process underway
    If ClockRunning = False Then
        If PauseState = False Then
            Counter = Val(MinSet.Text) * 60
            Timer.Interval = 1000
            Timer.Enabled = True
      ‘simplify display construct here…
            startTime.Text = Format$(Time, "00:00:00")  
            endtime.Text = Format$(DateAdd("n", Val(MinSet.Text), Time), "hh:mm:ss")
            clockimg.Visible = True
            Display.Show
            start.Caption = "Pause"
            ClockRunning = True
            MinSet.Enabled = False
            MinSet.Locked = True
        Else
            Debug.Print "I don’t believe I’ll ever hit this condition"
            Timer.Enabled = True
            PauseState = False
            ClockRunning = True
            start.Caption = "Pause title, being changed from what?"
        End If
    Else ' clock is underway, button hit = pausing or unpausing clock
'       "<<<<<<<<<<<HERE>>>>>>>>>>>>>>>"
        If PauseState = True Then ' user pressed 'pause' button
            start.Caption = "Pause"
            PauseState = False
            Timer.Enabled = True
            TimerDuringPause.Enabled = False
        Else 'user pressed 'resume' button
            start.Caption = "Resume"
            PauseState = True
            Timer.Enabled = False
            TimerDuringPause.Enabled = True
        End If
       
    End If
       
End Sub

' at this point, you'll want to put code in to disable and enable various buttons, and perhaps when the STOP button is pressed, kill the second timer if it too is running, or kill the stop button when you pause, I dunno...


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question