2 Replies - 938 Views - Last Post: 09 May 2013 - 03:54 AM Rate Topic: -----

#1 Correctness  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-May 13

Using a variable in the name of an object

Posted 09 May 2013 - 12:53 AM

So say I had a program with several pictureboxes, named pic1, pic2, pic3,... and so on, is there a way to refer to specific pictureboxes by replacing the number with a variable?

i.e.
 for count = 1 to 5
         piccount = someimage
       next count 


Basically I need some way of including in integer variable in the name of a picturebox or any other object, as a way of using a loops to change many objects at once.

Hopefully i'm explaining this well enough, thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Using a variable in the name of an object

#2 vks.gautam1  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 317
  • Joined: 21-March 08

Re: Using a variable in the name of an object

Posted 09 May 2013 - 01:25 AM

i think this may help you .
http://msdn.microsof...(v=vs.100).aspx
This is what i done
Dim ta
        For Each ta In Me.Controls
            If TypeOf ta Is TextBox Then
                If ta.textbox = String.Empty Then
                    MsgBox("Pls fill the data")
                    Exit Sub
                End If
            End If

        Next


Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,589
  • Joined: 12-December 12

Re: Using a variable in the name of an object

Posted 09 May 2013 - 03:54 AM

You can refer to to pic1, pic2 by concatenating the number at the end of "pic":

For count = 1 To 5
    Me.Controls("pic" & count.ToString).Image = whatever
Next count

This is a little messy though and it would be better to, for example, place the pictureboxes in a Panel and loop through the panel's controls collection.

For Each pix In Panel1.Controls
    If TypeOf pix Is PictureBox Then

    End If
Next

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1