Results 1 to 7 of 7

Thread: I need help with "for each"

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Québec, Canada
    Posts
    284

    I need help with "for each"

    I have this code and i would like to reduce it with a for each instruction, is there a way?

    Shape1.BackColor = vbRed
    Shape1.BorderColor = vbGreen
    Shape2.BackColor = vbRed
    Shape2.BorderColor = vbGreen
    Shape3.BackColor = vbRed
    Shape3.BorderColor = vbGreen
    Shape4.BackColor = vbRed
    Shape4.BorderColor = vbGreen
    Shape5.BackColor = vbRed
    Shape5.BorderColor = vbGreen
    Shape6.BackColor = vbRed
    Shape6.BorderColor = vbGreen
    Shape7.BackColor = vbRed
    Shape7.BorderColor = vbGreen
    Shape8.BackColor = vbRed
    Shape8.BorderColor = vbGreen
    Shape9.BackColor = vbRed

    Thanx

  2. #2
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    Try this

    VB Code:
    1. dim c as control
    2.  
    3. for each c in me.controls
    4.     if typename(c) = "Shape" then
    5.         c.backcolor = vbred
    6.         c.bordercolor = vbgreen
    7.     end if
    8. next c

  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    VB Code:
    1. Dim lctrl As Control
    2. For Each lctrl In Controls
    3.    If TypeOf lctrl Is Shape Then
    4.       lctrl.backcolor = vbRed
    5.       lctrl.forecolor = vbGreen
    6.    End If
    7. Next

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Code:
    Dim ctl as control
    
    For each ctl in form1.controls
        if typeof ctl is Shape then 
             .BackColor = vbRed 
             .BorderColor = vbGreen 
        End If
    Next ctl
    You're having problems as it's a control name, there are 2 solutions with this one - the above, looping through all the forms controls, or to put all the shapes in a control array and loop through these shape(i).backcolor etc.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Québec, Canada
    Posts
    284

    Thumbs up

    Thank you very much!!

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Damn, well and truly beaten to that one

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Another nice and easy way to do this would be like this:

    VB Code:
    1. For i = 1 To 9
    2. Controls("Shape" & i).BackColor = vbRed
    3. Controls("Shape" & i).BorderColor = vbGreen
    4. Next
    Baaaaaaaaah

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