Increment control name using a loop??
HI All
I have a problen I would like to resolve?
I am relatively new to Visual basic, so please bare with me,
I have a number of controls on a form all off which are simple shapes, that I would like to change the fill colour on incrementaly, in the manner of an LED bar display.
So I have Shape1, Shape2, Shape3........ Shape10
how would i go about incrementing the shape names that is how do I replace the numbers in the above example with a variable I can increment?
Many thanks for your help
zippy483
Re: Increment control name using a loop??
You can create a control array. Select Shape1. Rename it to something, Sheep for example. Then look at Index property. Write 0. Now you have created a control array! If you rename any other shapes to Sheep, they then get a new Index number of their own.
After this you have Sheep(0), Sheep(1), Sheep(2), Sheep(3) ... Sheep(9).
You can search the FAQ forum for more information about control arrays.
Re: Increment control name using a loop??
A control array is the best way to do it but you can also get away with using the controls collection to access the shape properties. This is using a loop to change all the controls at once but you get the idea.
Code:
Private Sub Command1_Click()
Dim ctl As Control
Dim i As Integer
For i = 1 To 5
Set ctl = Controls("Shape" & i)
ctl.FillStyle = vbFSSolid
ctl.FillColor = vbRed
Set ctl = Nothing
Next i
End Sub
Re: Increment control name using a loop??
Done that using a control array as suggested and appears to work ok
but i need to insert a pause such that
I get the following effect
Start Loop
Change attribute
Pause
Loop Back
I have tried using the sleep function within the loop but it doesn't appear to do what I want
what i am trying to achieve is a virtual scrolling led bar (a la knight rider :))
Re: Increment control name using a loop??
You can use a Timer control to perform the operation at specific intervals(that makes a delay)...:wave:
Re: Increment control name using a loop??
How is that achieved, like this
Start Loop
Change attribute
Timer
Loop Back
How do I assess when the timer has elapsed
i assume I start the time with Timer1.Enabled = True but how do I evaluate when the interval has passed
Re: Increment control name using a loop??
You have it all in a Timer. Here is a sample project, the same idea works with shapes too:
1) Start a new project
2) Add a Timer
3) Paste the following code:
Code:
Option Explicit
Private Sub Form_Load()
Timer1.Interval = 200
End Sub
Private Sub Timer1_Timer()
' remember this string each time
Static LED As String * 10
' is the first character other than NULL
If AscW(LED) Then
' initialized: move last character to be the first
Mid$(LED, 1, 10) = Right$(LED, 1) & Left$(LED, 9)
Else
' first time
LED = "1000000000"
End If
' update caption
Me.Caption = LED
End Sub
So the Timer code is self-contained, it just keeps working until it is stopped by setting Interval = 0.