Re: Multiple Timer Advice



THANKS SO MUCH ALL. Your posts have been tremendous. I have played with VB
a bit; more on the VBA side of the house and so I am learning. Being able
to break down your guys code that you added and trying to understand it has
helped out a great deal. The few thoughts I have that I want to accomplish
and will work to add are:
1. that I would like a way to reset each button as there are times when a
user only talks on the phone for 10 minutes so once done, the timer can be
reset so when the next user comes in it is ready to go.
2. I am thinking I may want the counter to count up instead of down because
there are also times when there is no line so we let the users stay on the
phones or computers as long as they want or until a line starts to form. By
making the timer move upward, I can then see who has been on the longest
when the line does start to form and can let them know their time is up.
3. I am also going to add some way for the times to be changed by the
person monitoring the system as there are some locations where the time
limits are different. Some MWR centers use a 15 minute time limit, some use
a 20 and some even use a 30 minute time limit. Would I accomplish this by
using an Setup form that goes invisible but is there to hold values?
I am building this as a learning tool, but if I test it out at our MWR site
and it works, I am going to send it to other sites here in Iraq and see if
they may want to use it.

I saw it mentioned about tracking time on the phone and such and initially I
thought of that as well, but then figured this is just a program to help the
monitors get the job done more efficiently without having to scan up and
down a sheet of paper comparing times.

I again thank you all so much for your ideas. I will work through this and
keep you all posted as to my progress by throwing the code into this message
thread. For anyone who would like to see and critique my finished product,
drop me an Email at walterREMOVETHIS.steadman@xxxxxxxxxxx
Thanks again very much.

Wally Steadman

"Rick Rothstein" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx> wrote in message
news:Oc3wEg7XFHA.1404@xxxxxxxxxxxxxxxxxxxxxxx
>> > > I don't know much about timers in VB but figure that I will have
> to
>> > > have 30 command buttons with some text boxes etc... and was
>> > > looking for ideas as to **the best approach** to do this type of
> task,
>> > > especially the application of having 30 timers working together.
>>
>> (I added emphasis)
>>
>> As Jerry did, I also think using a single timer would be appropriate
> here....
>
> One Timer control it is. <g>
>
> Start a new project and place a TextBox, Label, CommandButton and Timer
> on the form (don't worry about size or location...the code will handle
> that stuff). Change the following properties in the Properties Box:
>
> -- Change the CommandButton's Style property to 1-Graphical
>
> -- Change the Index properties of the CommandButton and the
> Label 0 (zero). (Do NOT change the Index for the TextBox or Timer.)
>
> Copy and then Paste the following code into the form's code window; then
> read how the program works in the text after the code.
>
> '**************************Start of Code**************************
> Option Explicit
>
> Private Const NumOfCols As Long = 5
> Private Const NumOfPhones As Long = 10
> Private Const NumOfComputers As Long = 20
> Private Const PhoneTimeLimit As String = "20:00"
> Private Const ComputerTimeLimit As String = "20:00"
>
> Private Const GrayColor As Long = &HE0E0E0
> Private Const PauseColor As Long = &H80C0FF
>
> Private Sub Command1_MouseDown(Index As Integer, Button As Integer, _
> Shift As Integer, X As Single, Y As Single)
> Dim Answer As String
> Dim OldCaption As String
> With Command1(Index)
> If Shift = vbCtrlMask Then
> If Button = vbRightButton Then
> With Command1(Index)
> .Caption = "OFF"
> .BackColor = GrayColor
> .Tag = ""
> End With
> End If
> ElseIf Shift = vbAltMask Then
> OldCaption = .Caption
> '
> ' Remove the next two lines, create your own input
> ' form and use it to set the value for the Answer
> ' variable and test for it being in the proper format.
> '
> Answer = Right$("0" & InputBox("Change Time"), 5)
> If Not Answer Like "##:##" Then Answer = OldCaption
> .Caption = Answer
> If .Caption = "OFF" Then
> .BackColor = GrayColor
> Else
> ColorButton Index
> End If
> ElseIf Button = vbRightButton Then
> If .BackColor = PauseColor Then
> .Caption = .Tag
> ColorButton Index
> ElseIf .Caption <> "OFF" Then
> .Tag = .Caption
> .Caption = "PAUSED (" & .Tag & ")"
> .BackColor = PauseColor
> End If
> ElseIf Command1(Index).Caption = "OFF" Then
> If Index < NumOfComputers Then
> Command1(Index).Tag = ComputerTimeLimit
> Else
> Command1(Index).Tag = PhoneTimeLimit
> End If
> Command1(Index).Caption = Command1(Index).Tag
> ColorButton Index
> End If
> If Label1(Index).ForeColor = vbRed And _
> Left$(.Caption, 6) <> "PAUSED" Then
> Label1(Index).ForeColor = vbBlack
> End If
> End With
> Text1.SetFocus
> End Sub
>
> Private Sub ColorButton(Index As Integer)
> Const OK_Green As Long = &HD0FFD0
> Const Warning_Yellow As Long = vbYellow
> Const Cautionary_Red As Long = &HD0D0FF
> Const OutOfTime_Red As Long = vbRed
> With Command1(Index)
> If .Caption > "05:00" Then
> .BackColor = OK_Green
> ElseIf .Caption > "02:00" Then
> If .BackColor <> Warning_Yellow Then
> If .BackColor <> PauseColor Then
> Beep
> Label1(Index).ForeColor = vbRed
> End If
> .BackColor = Warning_Yellow
> End If
> ElseIf .Caption > "00:00" Then
> If .BackColor <> Cautionary_Red Then
> If .BackColor <> PauseColor Then
> Beep
> Label1(Index).ForeColor = vbRed
> End If
> .BackColor = Cautionary_Red
> End If
> Else
> .BackColor = OutOfTime_Red
> Beep
> Label1(Index).ForeColor = vbRed
> End If
> End With
> End Sub
>
> Private Sub Timer1_Timer()
> Dim CB As CommandButton
> For Each CB In Command1
> With CB
> If Left$(.Caption, 6) <> "PAUSED" And .Caption <> "OFF" And _
> .Caption <> "00:00" Then
> .Caption = Format(DateAdd("s", -1, "00:" & .Caption), "nn:ss")
> ColorButton CB.Index
> End If
> End With
> Next
> End Sub
>
> Private Sub Form_Activate()
> Text1.SetFocus
> End Sub
>
> Private Sub Form_Load()
> Dim Idx As Long
> Dim ButtonIndex As Long
> Dim PhoneButtonOffset As Long
> Dim ButtonWidth As Long
> Const LabelLeftOffset = 150
> Const ButtonMargin = 120
> Const Spacer = 60
> Const DarkGrayColor = &HC4C4C4
> Me.AutoRedraw = True
> Me.BackColor = GrayColor
> With Label1(0)
> .Font.Name = "Arial"
> .Font.Bold = True
> .Font.Size = 10
> .BackColor = GrayColor
> .ForeColor = vbBlack
> .Caption = "Computer #1"
> .Tag = "00:00"
> Set Me.Font = .Font
> With Command1(0)
> .Width = Me.TextWidth("PAUSED (XX:XX)") + 2 * Spacer
> .Height = 2 * Me.TextHeight("X")
> End With
> .Move LabelLeftOffset, ButtonMargin, Command1(0).Width, _
> Me.TextHeight("x") + Spacer
> Command1(0).Move ButtonMargin, .Top + .Height
> End With
> With Command1(0)
> Set .Font = Label1(0).Font
> .Caption = "OFF"
> Text1.Move .Left + 3 * Screen.TwipsPerPixelX, _
> .Top + 3 * Screen.TwipsPerPixelY, 10, 10
> Text1.ZOrder 1
> End With
> Timer1.Interval = 1000
> Timer1.Enabled = True
> Me.Caption = "Computer/Phone Timer"
> For Idx = 1 To NumOfComputers + NumOfPhones - 1
> Load Label1(Idx)
> Load Command1(Idx)
> If Idx = NumOfComputers Then
> With Command1(Idx - 1)
> Me.Line (Command1(0).Left, .Top + 1.3 * .Height)- _
> (Command1(NumOfCols - 1).Left + _
> Command1(NumOfCols - 1).Width, .Top + _
> 1.5 * .Height), DarkGrayColor, BF
> End With
> End If
> With Label1(Idx)
> If Idx < NumOfComputers Then
> ButtonIndex = Idx Mod NumOfCols
> .Move LabelLeftOffset + (ButtonIndex) * _
> (Command1(0).Width + 4 * Spacer), _
> ButtonMargin + (Idx \ NumOfCols) * _
> (.Height + Command1(0).Height + 3 * Spacer)
> .Caption = "Computer #" & CStr(Idx + 1)
> Else
> ButtonIndex = (Idx - NumOfComputers) Mod NumOfCols
> PhoneButtonOffset = Command1(0).Height / 2
> .Move LabelLeftOffset + (ButtonIndex) * _
> (Command1(0).Width + 4 * Spacer), _
> ButtonMargin + Command1(NumOfComputers - 1).Top + _
> Command1(NumOfComputers - 1).Height + _
> ((Idx - NumOfComputers) \ NumOfCols) * _
> (.Height + Command1(0).Height + 3 * Spacer) + _
> PhoneButtonOffset
> .Caption = "IP Phone #" & CStr(Idx - (NumOfComputers - 1))
> End If
> .Visible = True
> End With
> With Command1(Idx)
> .Move Label1(Idx).Left - (LabelLeftOffset - ButtonMargin), _
> Label1(Idx).Top + Label1(Idx).Height
> .Caption = "OFF"
> .BackColor = GrayColor
> .Visible = True
> End With
> Next
> Me.Width = Command1(NumOfCols - 1).Left + _
> Command1(NumOfCols - 1).Width + _
> LabelLeftOffset + (Me.Width - Me.ScaleWidth)
> Me.Height = Command1(NumOfComputers + NumOfPhones - 1).Top + _
> Command1(0).Height + LabelLeftOffset + _
> (Me.Height - Me.ScaleHeight)
> Me.Move (Screen.Width - Me.Width) / 2, _
> (Screen.Height - Me.Height) / 2
> End Sub
>
> Private Sub Form_Unload(Cancel As Integer)
> Dim cntrl As Control
> For Each cntrl In Controls
> If Not TypeOf cntrl Is TextBox And _
> Not TypeOf cntrl Is Timer Then
> If cntrl.Index > 0 Then Unload cntrl
> End If
> Next
> End Sub
> '**************************End of Code**************************
>
> The first 5 constants defined at the top of the code control the entire
> layout. These constants are preset to the values you specified in your
> message, but you can change them as or when needed (like if you get more
> computers or phones). These constants allow you to specify how many
> columns will show on the form, the number of phones and computers in
> your facility and the time limits for each of them (that means, you can
> set one time limit for the computers and a different one for the
> phones). The TextBox is there, but hidden, so that it can continually
> receive the focus (this allows makes it so no CommandButton ever shows
> the focus rectangle (I think it looks neater that way).
>
> Now, run the program. The program is very visually oriented and I think
> you will like it. To start a timer for one of the labeled computers or
> phones, simply left-click on its button. The button turns a light shade
> of green and the timer begins counting down. When the timer reaches 5
> minutes remaining, the button will change to a yellow, the computer or
> phone label will turn red and a beep will sound. The beep is to get your
> attention and the red label to indicate you need to warn the user that
> less than 5 minutes remain. Once you have warned the user of his time,
> left-click the button to make the label's color black again. When the
> timer reaches 2 minutes remaining, but button will change to a light
> shade of red and, again, the label will turn red and a beep will sound.
> As before, the beep is to alert you and the red label indicates which
> timer has crossed the warning time boundary. Click the button to change
> the label's color back to black again. Finally, when the time fully
> expires, the countdown stops, a beep sounds and the button and label
> turn red. In case you are wondering why I chose the color scheme that I
> did, I modeled it after the colors in a traffic light.
>
> To turn the timer off (if it is running) and change button's caption
> back to the default OFF text string (whether it was running or the time
> had fully expired), press the Control Key in combination with a Right
> Mouse Click on the button. This action is immediate, so be careful (it
> is the only key combination that uses the Control Key). You can pause
> any timer at any time by right clicking on the button. The button will
> turn an orange color and display the word PAUSED along with the
> remaining time (in parentheses) that remains on the timer. Right
> clicking on a paused timer will restart the timer again at the point it
> left off at. If the label is red (indicating that you need to warn the
> user of the shortness of his remaining time), right-clicking on the
> button still pauses the timer and the label remains red in color;
> however, right clicking again to restart the timer will turn the label's
> color to black (the assumption being you paused the timer in order to
> walk over and warn the user; un-pausing the timer means you in fact did
> warn him).
>
> Finally, you can change the remaining time for any timer to any value
> you want by pressing the Alt Key in combination with a Mouse Click
> (either right or left mouse button). Type in the new countdown time
> using one or two digits followed by a colon followed by two more digits.
> If you type in an incorrectly formatted string, the timer continues from
> the last value it had if it is running. A word of caution about how I
> implemented this feature... I used an InputBox in the MouseDown event
> procedure in order to save time and to keep from having to design a
> second form for you (how I would like take in the time value probably
> isn't how you would want to do it). So,you must, let me repeat that, you
> MUST remove the InputBox line (I placed a noticeable comment immediately
> before the InputBox statement to help you find it) and create a 2nd
> non-modal form in the project to handle this function. Why can't you the
> InputBox as I now have it? Because calling it up freezes ALL of the
> timers until it is dismissed.
>
> Okay, I think I have described everything that is in the program. You
> should realize that I developed the code for it off the top of my head
> and I typed, edited and debugged the code directly on the screen. I
> didn't pre-design it as I would for a for-pay project, so I don't want
> anyone complaining that I shouldn't have done this or that I should have
> done that instead. I know it is not a thing of beauty<g>, but it does
> work. Enjoy!
>
> Rick - MVP
>


.



Relevant Pages

  • Re: SYSTEM.TIMERS.TIMER pausieren lassen
    ... Private Sub Timer1_TickHandles Timer1.Tick ... Dim dNow As Date = Now ... Dim iDiffA As Integer ... 'This routine calculates the remaining seconds for a specific timer. ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Stop Timer
    ... Private Sub cmdUntilLoop_Click ... Dim intPause As Integer ... MsgBox Timer ... A more elegant way is to have a public variable or control on the form that can be set from outside the loop, and that will be tested in each iteration of the loop to see if it's time to stop. ...
    (microsoft.public.access.modulesdaovba)
  • Array VBA
    ... I have a timer on a subform on a main "order form." ... Dim TotalElapsedMilliSec As Long ... Private Sub cmdStartStop_Click ... Dim Minutes As String ...
    (microsoft.public.access.modulesdaovba)
  • Re: Multiple Timer Advice
    ... One Timer control it is. ... Private Const NumOfPhones As Long = 10 ... Dim OldCaption As String ... Private Sub ColorButton ...
    (microsoft.public.vb.general.discussion)
  • Re: Scrolling text in a stationary label help needed
    ... I had to add a check in the timer event to ... Dim xposition As Long ... Private Sub Form_KeyPress ... In your example the code isn't runnng in a Timer at ...
    (microsoft.public.vb.general.discussion)