Results 1 to 6 of 6

Thread: Reducing code, dynamic names

Threaded View

  1. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Reducing code, dynamic names

    Quote Originally Posted by MarkT View Post
    See if this helps
    Code:
    Private Sub Command1_Click()
    Dim i As Integer
    Dim pic As PictureBox
    
        For i = 1 To 3
            Set pic = Controls("Picture" & i)
            pic.Visible = Not pic.Visible
        Next i
    End Sub
    You don't need the variable; controls can be accessed by name directly.
    Code:
    Private Sub Command1_Click()
        Dim i As Integer
    
        For i = 1 To 3
            Me("Picture" & i).Visible = False
        Next
    End Sub
    EDIT: Though in looking back at this I'm guessing you already know this and only used a variable because you're referencing the control twice. (Visible = Not Visible) I suppose if you wanted to keep the code clean and save a variable you could use a With...End With block.
    Last edited by Ellis Dee; Apr 22nd, 2009 at 04:25 AM.

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