Assigning a Picture to a Picture Box Using Variables

Hi,

I'm sure this is pitifully simple, but I can't for the life of me figure it out. I want to use variables to both refer to a picture box and to refer to the image assigned to it. For example:

PictureBox21.Image = My.Resources.Resources._131

I want to replace the "21" and the "_131" with variables so that I can use a for...next loop to assign a bunch of different bitmaps to a bunch of different picture boxes.

I keep getting an error that says "PictureBox is a type and cannot be used as an expression".

What's the syntax for doing this

Thanks



Answer this question

Assigning a Picture to a Picture Box Using Variables

  • Marcos Germano

    Bobbrt1329 (What a cute name ! )

    Check out the Imagelist control. Once you understand what it is... you'll go crazy.



  • 钟诚

    It looks like that does it. Thanks a bunch.

    Bob


  • Newbian

    Simple Code here which will assign resources called Image1 and Image2 to some pictureboxes called Picturebox1 and Picturebox2 on the form. This is probably the basis of what your looking for.

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim x As String = "Image"
    Dim i As Integer

    For i = 1 To 2
    CType(Me.Controls("Picturebox" & i.ToString), PictureBox).Image = CType(My.Resources.ResourceManager.GetObject(x & i.ToString), System.Drawing.Image)
    Next i


    End Sub
    End Class


  • Assigning a Picture to a Picture Box Using Variables