Results 1 to 6 of 6

Thread: Reducing code, dynamic names

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    28

    Exclamation Reducing code, dynamic names

    Another simple question that i'm not familiar with in vb

    In say actionsctipt etc i can reduce a lot of code by creating dynamic variable names etc

    How to i transform somthing like this:
    Code:
    PictureBox1.Visible = False
    PictureBox2.Visible = False
    PictureBox3.Visible = False
    Into something like this, (which i know is very wrong!):
    Code:
    Dim i As Integer
    For i = 1 To 3
    "PictureBox"&i = False
    Next
    Any help is much appreciated

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

    Re: Reducing code, dynamic names

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    28

    Re: Reducing code, dynamic names

    What if its just a variable that i want to do?

    like LED1, LED2, LED3

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

    Re: Reducing code, dynamic names

    Create a control array:
    • Create a picturebox.
    • Change it's Name to PB.
    • Change it's Index to 0.
    • Copy PB to clipboard.
    • Paste. The new control gets Index 1.
    • Paste. The new control gets Index 2.


    You now have three pictureboxes, all have the name PB. You can access the first one with PB(0), second with PB(1) and third with PB(2).

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Reducing code, dynamic names

    What if its just a variable that i want to do?
    try using an array
    vb Code:
    1. dim led() as string
    2. redim led(3)   ' 0 to 3 = 4 elements
    3. for i = 0 to ubound(led)
    4.      led(i) = "element " & I
    5. next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    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