13 Replies - 6518 Views - Last Post: 12 April 2011 - 04:26 PM Rate Topic: -----

#1 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 130
  • Joined: 03-April 11

Using the timer control to launch an automatic sequence

Posted 10 April 2011 - 04:35 AM

In the program, when I click a command button, colours must change automatically in a sequence. E.g. I click the command button, and the form changes to red; after 1 second, it changes to green; after 1 second, it changes to blue; then it stops. I am supposed to use the Timer control... (Interval will obviously be 1000, since after each second the colour must change). I don't know how to include the Timer's function in my code... Any help would greatly relieve me. I'm having a headache :P.

Is This A Good Question/Topic? 0
  • +

Replies To: Using the timer control to launch an automatic sequence

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Using the timer control to launch an automatic sequence

Posted 10 April 2011 - 04:46 AM

its:

Private Sub Timer1_Timer()
'Put your code here when the interval pass and the timer ticks'
End Sub




its rly easy to make the event on the timer. draw the timer in your form and just dbl click on it :)
Was This Post Helpful? 1
  • +
  • -

#3 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 130
  • Joined: 03-April 11

Re: Using the timer control to launch an automatic sequence

Posted 10 April 2011 - 06:38 AM

Thank you, that greatly helped (and relieved) me. I was putting the code in the command button's Click event -_- I didn't know it had to be in the Timer's event... Stupid of me :P. Now, I am having another problem. As I said, the form's backcolour must change to red, then to green, then to blue. It changes to red then to green, but it stops at green o.O. I can't figure out what is missing in my code... though I'm sure it's very simple. See code below.

Private Sub tmrTimer_Timer()
    Static Interval As Integer
        
    If Interval = 0 Then
        Interval = tmrTimer.Interval
    Else
        Interval = tmrTimer.Interval + 1000
    End If
    
    Select Case Interval
        Case Is = 1000
            Form1.BackColor = RGB(255, 0, 0)
        Case Is = 2000
            Form1.BackColor = RGB(0, 255, 0)
        Case Is = 3000
            Form1.BackColor = RGB(0, 0, 255)
    End Select


It's like the Interval doesn't ever get to the value "3000". I guess it's something to do with the If construct... I just want to know what is the problem with my code. By the way, the Timer's Interval property is set to 1000.
Was This Post Helpful? 0
  • +
  • -

#4 chuckjessup  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 380
  • Joined: 26-October 09

Re: Using the timer control to launch an automatic sequence

Posted 10 April 2011 - 12:11 PM

View PostAN1554, on 10 April 2011 - 06:38 AM, said:

Thank you, that greatly helped (and relieved) me. I was putting the code in the command button's Click event -_- I didn't know it had to be in the Timer's event... Stupid of me :P. Now, I am having another problem. As I said, the form's backcolour must change to red, then to green, then to blue. It changes to red then to green, but it stops at green o.O. I can't figure out what is missing in my code... though I'm sure it's very simple. See code below.

Private Sub tmrTimer_Timer()
    Static Interval As Integer
        
    If Interval = 0 Then
        Interval = tmrTimer.Interval
    Else
        Interval = tmrTimer.Interval + 1000
    End If
    
    Select Case Interval
        Case Is = 1000
            Form1.BackColor = RGB(255, 0, 0)
        Case Is = 2000
            Form1.BackColor = RGB(0, 255, 0)
        Case Is = 3000
            Form1.BackColor = RGB(0, 0, 255)
    End Select


It's like the Interval doesn't ever get to the value "3000". I guess it's something to do with the If construct... I just want to know what is the problem with my code. By the way, the Timer's Interval property is set to 1000.



That only works for as many timer ticks that you select case for, what you need is to use a variable, increment the variable per timer tick and code for each tick. like so
'General'
private X as long
'timer'
Private Timer1_Timer()
x=x+1
if x=1 then form1.backcolor=rgb(255,0,0)
if x=2 then form1.backcolor=rgb(0,255,0)
if x=3 then form1.backcolor=rgb(0,0,255)
'to loop, set x to 0'
x=0
'or end timer, not both...'
if x=4 then timer1.enabled=false
'you can loop a number of times and then disable the timer by adding a second long integer, say Y... each complete pass of X add 1 to Y, Then when y=whatever set timer1.enabled=false



X and if you choose Y, or any variable that you have in the timer event that is changed during the Timer event have to be declared in the general decs for perpetuation of the variable If you dont declare X in General it will just do 1. just so you know.

That should work, and is easy to work with...

Hope that helped...
Jesse Fender

This post has been edited by chuckjessup: 10 April 2011 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Using the timer control to launch an automatic sequence

Posted 10 April 2011 - 09:56 PM

Here's a simplification which is also more efficient:
Sub tmrTimer_Timer()
Select Case Me.BackColor
    Case vbRed
        Me.BackColor = vbGreen
    Case vbGreen
        Me.BackColor = vbBlue
    Case vbBlue
        Timer1.Enabled = False
    Case Else
        Me.BackColor = vbRed
End Select
End Sub

To get all the color constants that are available, look in the Object Browser.

An, to clarify why you have a problem with yours: Interval just means how often tmrTimer_Timer gets called. Your tmrTimer.Interval never changes from 1000. So, on your first pass, your static property Interval = 0. So it gets set to 1000. Therefore, on your first pass BackColor changes to Red. Second pass, Interval = 1000. Therefore, it gets set to tmrTimer.Interval + 1000, or 2000. Color changes to Green. Next pass, Interval = 2000. Therefore, it gets set to tmrTimer.Interval (which is still 1000, you haven't changed it at all) + 1000, just as before. Color remains Green. And so on ad infinitum.

You don't want to mess with the interval anyway, because you want the timer to fire every second. Each second, you change BackColor to a value that is based on what it is "now." So, just evaluate each possible BackColor value, and do what you need to do when it is that value.

Jesse, An's use of a static interval would also work better in your solution. The point about a static interval is that the scope is local (can only be seen in the sub it's in) but the lifetime is the same as the module (lasts as long as the form). That way, it can only be seen inside the sub, but whatever value gets set in a call is still the value next time you call the sub. (Another way of explaining is that static variables don't get reset when you leave the sub.) Since you don't need to see the value anywhere but in the sub, but need the value to persist between calls to it, you have the situation where a static variable is to be recommended. This way, none of your other functions can inadvertently change the value and mess up your code.

This post has been edited by BobRodes: 10 April 2011 - 10:10 PM

Was This Post Helpful? 1
  • +
  • -

#6 chuckjessup  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 380
  • Joined: 26-October 09

Re: Using the timer control to launch an automatic sequence

Posted 11 April 2011 - 01:14 AM

As far as my reply, you can use the vbcolors, or even hexcolor codes

it would look something like:
if x=1 then form1.backcolor=vbblue

or
if x=2 then form1.backcolor=&H8000000F& 'default form color'

see its not a bad snipit.

As far as the interval thing, you cant change it with the timer enabled, it will not take effect till you stop the timer, clear x (set x to 0) and restart the timer... That is a major pain in the butt...

Jesse Fender
Was This Post Helpful? 0
  • +
  • -

#7 chuckjessup  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 380
  • Joined: 26-October 09

Re: Using the timer control to launch an automatic sequence

Posted 11 April 2011 - 01:26 AM

View PostBobRodes, on 10 April 2011 - 09:56 PM, said:

Jesse, An's use of a static interval would also work better in your solution. The point about a static interval is that the scope is local (can only be seen in the sub it's in) but the lifetime is the same as the module (lasts as long as the form). That way, it can only be seen inside the sub, but whatever value gets set in a call is still the value next time you call the sub. (Another way of explaining is that static variables don't get reset when you leave the sub.) Since you don't need to see the value anywhere but in the sub, but need the value to persist between calls to it, you have the situation where a static variable is to be recommended. This way, none of your other functions can inadvertently change the value and mess up your code.


Well I dont generally use letters as my timer variables, the typical variable that i use for my timer is something TimerCount as a long integer.

My form loads always set the timer interval on the timers on that form... If you dont want to mix up what variables are used for timers, make it clear that the variable is for the timer controls...

The X in my example was declared in the General Declarations, and then the timer control deals with adding or removing the specified number from x. Simple.

Jesse Fender
Was This Post Helpful? 0
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Using the timer control to launch an automatic sequence

Posted 11 April 2011 - 06:19 PM

View Postchuckjessup, on 11 April 2011 - 09:14 AM, said:

As far as my reply, you can use the vbcolors, or even hexcolor codes

it would look something like:
if x=1 then form1.backcolor=vbblue

or
if x=2 then form1.backcolor=&H8000000F& 'default form color'

see its not a bad snipit.

As far as the interval thing, you cant change it with the timer enabled, it will not take effect till you stop the timer, clear x (set x to 0) and restart the timer... That is a major pain in the butt...

Jesse Fender

I'm sorry, I didn't mean to say it was a bad snippet. Any one of the above works, and your code does what it's supposed to do. However, I wanted to point out that the constants are available, that select case was preferable to if then, and that you don't need to maintain a variable to keep track of how many times the sub was accessed.
Was This Post Helpful? 0
  • +
  • -

#9 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Using the timer control to launch an automatic sequence

Posted 12 April 2011 - 09:30 AM

View Postchuckjessup, on 11 April 2011 - 09:26 AM, said:

View PostBobRodes, on 10 April 2011 - 09:56 PM, said:

Jesse, An's use of a static interval would also work better in your solution. The point about a static interval is that the scope is local (can only be seen in the sub it's in) but the lifetime is the same as the module (lasts as long as the form). That way, it can only be seen inside the sub, but whatever value gets set in a call is still the value next time you call the sub. (Another way of explaining is that static variables don't get reset when you leave the sub.) Since you don't need to see the value anywhere but in the sub, but need the value to persist between calls to it, you have the situation where a static variable is to be recommended. This way, none of your other functions can inadvertently change the value and mess up your code.


Well I dont generally use letters as my timer variables, the typical variable that i use for my timer is something TimerCount as a long integer.

My form loads always set the timer interval on the timers on that form... If you dont want to mix up what variables are used for timers, make it clear that the variable is for the timer controls...

The X in my example was declared in the General Declarations, and then the timer control deals with adding or removing the specified number from x. Simple.

Jesse Fender

I'm not making myself clear here, Jesse. I'm not making a point about naming conventions, I'm making a point about economy of scope. Best practice is to avoid letting a variable be seen where it doesn't need to be. Now, the X in your example (call it what you will) is better declared in the Sub itself as a static variable rather than in the General Declarations section as a private (or Dim, same thing as Private up there) variable. This is because the variable is accessed nowhere but in the sub. The point about static variables, again, is that any changes to their value persist between function calls.

Here's a simple example:
Option Explicit

Private Sub Command1_Click()
Dim i As Integer
On Error GoTo ErrHandle
For i = 0 To 7
    CallFive
Next i
Exit Sub
ErrHandle:
    Debug.Print Err.Number, Err.Source, Err.Description
End Sub

Private Sub CallFive()
Static HowMany As Integer
HowMany = HowMany + 1
If HowMany > 5 Then
    Err.Raise vbObjectError - 1, "CallFive", "Called CallFive more than five times"
End If
Debug.Print "Doing stuff " & CStr(HowMany) & " time" & IIf(HowMany = 1, ".", "s.")
End Sub


Which if run gives the following:

Doing stuff 1 time.
Doing stuff 2 times.
Doing stuff 3 times.
Doing stuff 4 times.
Doing stuff 5 times.
-2147221505 CallFive Called CallFive more than five times

This post has been edited by BobRodes: 12 April 2011 - 09:47 AM

Was This Post Helpful? 0
  • +
  • -

#10 chuckjessup  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 380
  • Joined: 26-October 09

Re: Using the timer control to launch an automatic sequence

Posted 12 April 2011 - 12:49 PM

View PostBobRodes, on 12 April 2011 - 09:30 AM, said:

View Postchuckjessup, on 11 April 2011 - 09:26 AM, said:

View PostBobRodes, on 10 April 2011 - 09:56 PM, said:

Jesse, An's use of a static interval would also work better in your solution. The point about a static interval is that the scope is local (can only be seen in the sub it's in) but the lifetime is the same as the module (lasts as long as the form). That way, it can only be seen inside the sub, but whatever value gets set in a call is still the value next time you call the sub. (Another way of explaining is that static variables don't get reset when you leave the sub.) Since you don't need to see the value anywhere but in the sub, but need the value to persist between calls to it, you have the situation where a static variable is to be recommended. This way, none of your other functions can inadvertently change the value and mess up your code.


Well I dont generally use letters as my timer variables, the typical variable that i use for my timer is something TimerCount as a long integer.

My form loads always set the timer interval on the timers on that form... If you dont want to mix up what variables are used for timers, make it clear that the variable is for the timer controls...

The X in my example was declared in the General Declarations, and then the timer control deals with adding or removing the specified number from x. Simple.

Jesse Fender

I'm not making myself clear here, Jesse. I'm not making a point about naming conventions, I'm making a point about economy of scope. Best practice is to avoid letting a variable be seen where it doesn't need to be. Now, the X in your example (call it what you will) is better declared in the Sub itself as a static variable rather than in the General Declarations section as a private (or Dim, same thing as Private up there) variable. This is because the variable is accessed nowhere but in the sub. The point about static variables, again, is that any changes to their value persist between function calls.

Here's a simple example:
Option Explicit

Private Sub Command1_Click()
Dim i As Integer
On Error GoTo ErrHandle
For i = 0 To 7
    CallFive
Next i
Exit Sub
ErrHandle:
    Debug.Print Err.Number, Err.Source, Err.Description
End Sub

Private Sub CallFive()
Static HowMany As Integer
HowMany = HowMany + 1
If HowMany > 5 Then
    Err.Raise vbObjectError - 1, "CallFive", "Called CallFive more than five times"
End If
Debug.Print "Doing stuff " & CStr(HowMany) & " time" & IIf(HowMany = 1, ".", "s.")
End Sub


Which if run gives the following:

Doing stuff 1 time.
Doing stuff 2 times.
Doing stuff 3 times.
Doing stuff 4 times.
Doing stuff 5 times.
-2147221505 CallFive Called CallFive more than five times

Well i have usually use timers for progress bars, and the like... i have tried using a declared variable in the procedure and it didn't work, so thats why i usually put them in the gen dec's Ill try again see if using statics works better...

Jesse Fender
Was This Post Helpful? 0
  • +
  • -

#11 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Using the timer control to launch an automatic sequence

Posted 12 April 2011 - 02:15 PM

Suppose you paste the code example I gave you and step through it. I think once you get what's happening, you'll like static variables too. :)
Was This Post Helpful? 0
  • +
  • -

#12 chuckjessup  Icon User is offline

  • D.I.C Regular

Reputation: 33
  • View blog
  • Posts: 380
  • Joined: 26-October 09

Re: Using the timer control to launch an automatic sequence

Posted 12 April 2011 - 02:54 PM

View PostBobRodes, on 12 April 2011 - 02:15 PM, said:

Suppose you paste the code example I gave you and step through it. I think once you get what's happening, you'll like static variables too. :)

Yeah I see what you mean...

I liked the conditional statement over if to add the s at the end of "time" thats neat, ill have to implement that in my apps...

cStr is a conversion to string right?

Jesse Fender
Was This Post Helpful? 0
  • +
  • -

#13 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Using the timer control to launch an automatic sequence

Posted 12 April 2011 - 04:12 PM

I use that instant if fairly often, maybe because I got my start in Lotus 1-2-3.
Was This Post Helpful? 0
  • +
  • -

#14 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Using the timer control to launch an automatic sequence

Posted 12 April 2011 - 04:26 PM

Also, since you're a fanatic, and since it's also on topic, have a look at this: http://support.microsoft.com/kb/180736 . The Timer control is actually a wrapper for these functions. Furthermore, I've always tended to resist the use of a control that is never visible at runtime.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1