Results 1 to 7 of 7

Thread: Increment control name using a loop??

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    9

    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

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    9

    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 )

  5. #5
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Increment control name using a loop??

    You can use a Timer control to perform the operation at specific intervals(that makes a delay)...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    9

    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

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

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