Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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?

 

 

  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.

Posted

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

 

 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.

 

   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:

 

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

 

 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.

  • *Experts*
Posted

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 :).

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...