Creating objects by variable name

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
I am trying to create a number of picture boxes, each one called PicBox_'x-cood'_'y-cood'. Think of it as a kind of table of pic'boxes.

I want these pic'boxes to be called with the coordinates, using something like:

Visual Basic:
Dim "PicBox_" & xpos & "_" & ypos As New PictureBox

Assuming you could declare objects, in the same way you would concat' strings. Is there a way of doing this?

I have enclosed the rest of the code from the proc., if it might help you understand what i am trying to do?


Visual Basic:
   Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      Dim Xpos As Short
      Dim Ypos As Short
      Dim MapSizeX As String
      Dim MapSizeY As String
      Dim aryMap(0, 0) As String

      MapSizeX = InputBox("How many tiles do you wish to have for the width of the map?")
      MapSizeY = InputBox("How many tiles do you wish to have for the height of the map?")

      ReDim aryMap(MapSizeX, MapSizeY)
      'MsgBox(MapSizeX & MapSizeY)

      For Xpos = 1 To MapSizeX
         For Ypos = 1 To MapSizeX

            Dim "PicBox_" & xpos & "_" & ypos As New PictureBox

'            .......rest of picbox stuff

         Next Ypos
      Next Xpos

   End Sub

Thanks.
 
Thanks. This is what i came up with.......

Visual Basic:
  Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      Dim Xpos As Short
      Dim Ypos As Short
      Dim MapSizeX As String
      Dim MapSizeY As String

      Dim aryMap(0, 0) As String

      MapSizeX = InputBox("How many tiles do you wish to have for the width of the map?")
      MapSizeY = InputBox("How many tiles do you wish to have for the height of the map?")

      ReDim aryMap(MapSizeX, MapSizeY)
      'MsgBox(MapSizeX & MapSizeY)

      For Xpos = 1 To MapSizeX
         For Ypos = 1 To MapSizeX
            Dim PicBox As New PictureBox

            PicBox.Name = "PicBox_" & Xpos & "_" & Ypos
            'PicBox_1_2.Name = "PicBox_1_2"
            PicBox.Size = New System.Drawing.Size(10, 10)
            PicBox.Location = New System.Drawing.Point(0 + 20 * Xpos, 0 + 20 * Ypos)
            PicBox.TabIndex = 0
            PicBox.TabStop = False
            PicBox.BackColor = Color.Red
            PanelTest.Controls.Add(PicBox) 'slightly out due to borders

         Next Ypos
      Next Xpos

   End Sub

It puts lots of red squares spaced out nicely on my page - which is what i expected it to do. But thinking about the requirements later on in the program, i will need to reference these Pic' Boxes again, and i cant see how i can do that. It is easy?

Alternatively, in another project I created a structure and put that structure into an array, like follows.

Visual Basic:
    Public Structure StructField
        Public FieldName As String
        Public FieldStartPos As Integer
        Public FieldLength As Integer
    End Structure

    Public Fields() As StructField

Then when i wanted to change something in the structure i do it as follows:

Visual Basic:
fields(i).fieldlength = "Pants"

Could i do that with Pic' Boxes as well? I have tried it, but i couldnt get it to work. I got as far as

Visual Basic:
  Public Structure StructPic
      Public PicBox As PictureBox
   End Structure

   Public aryPic(0, 0) As StructPic

But it wouldnt let me declare NEW Pic' Boxes inside of the structure, so when i went to call them in the settings, it didnt work, as it hadnt created an instance of them.
 
Dim pb As PictureBox
For Each pb In PanelTest.Controls
MessageBox.Show(pb.Name)
Next


(How do you guys get the code to look pretty in this forum?)
 
You could use the conecpt the JumpsInLava showed you or you could use an array list to store all the picture boxes and add or remove whenever you want using the same apporach shown in the JumpsInLava's post.

JumpsInLava, format the code use the vb tags:
[ vb ]
[ /vb ]
But without spaces, if I didnt use spaces it would format it and you wouldnt see the tags :).
 
Back
Top