When i hit the start button i just get white boxes and no image for the dynamic and the image does not show for the random image i get a red x wondering if anyone can help me thanks!

Dim r As New Random
        Dim s As String
        Dim n As Integer

        n = r.Next(0, 26)
        s = "tile"
        If n <= 9 Then s += "0"
        s += n.ToString

        pictray1.Image = My.Resources.Tile02
        pictray1.Image = My.Resources.ResourceManager.GetObject(s)

        s = Application.StartupPath & "tiles\" & s & ".jpg"
        pictray1.ImageLocation = s


        Dim i As Integer
        Dim j As Integer

        Dim buttonnum As Integer
        Dim mybutton As New PictureBox
        For i = 1 To 15
            For j = 1 To 15
                buttonnum = (j - 1) * 15 + i
                mybutton = New System.Windows.Forms.picturebox
                mybutton.Location = New System.Drawing.Point(i * 30, j * 30)
                mybutton.Name = "Button" & buttonnum
                mybutton.Size = New System.Drawing.Size(30, 30)
                mybutton.Text = buttonnum
                'mybutton.BackColor = Color.White
                Select Case buttonnum
                    Case 1, 8, 15, 106, 120, 211, 218, 225
                        mybutton = image.fromfile("tripleword.jpg")
                        'mybutton.Image = My.Resources.TripleWord
                        'mybutton.BackColor = Color.red
                    Case 4, 12, 37, 39, 46, 53, 60, 93, 97, 99, 103, 109, 117, 123, 127, 129, 133, 166, 173, 180, 187, 189
                        mybutton.Image = My.Resources.DoubleLetter
                        'mybutton.BackColor = Color.LightBlue
                    Case 21, 25, 77, 81, 85, 89, 137, 141, 145, 149, 201, 205
                        mybutton.Image = My.Resources.TripleLetter
                        'mybutton.BackColor = Color.Blue
                    Case 17, 29, 33, 43, 49, 57, 65, 71, 155, 161, 169, 177, 183, 193, 197, 209
                        mybutton.Image = My.Resources.DoubleWord
                        'mybutton.BackColor = Color.Pink
                End Select
                Me.Controls.Add(mybutton)

            Next
        Next

    End Sub

That's an error image i.e. some image can't be found.

At least mybutton.Image = Image.FromFile("tripleword.jpg") may not look for the image from the place you were thinking of. Also, you can't assign to picture box in itself, use Image property.

I cant use the image property since the boxes are generated after i hit the start button from the menu strip.

That's an error image i.e. some image can't be found.

Did you found a solution to this i.e. the original problem?

I cant use the image property since the boxes are generated after i hit the start button from the menu strip.

You mean you create picture box controls dynamically at the run time? Dynamically created controls do have all the same properties and methods as "statically" created controls. So, you still have the Image property and you have to use it.

ok i got the picture box but im having trouble with the random images. im just getting a red x box

Public Class Form1

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim r As New Random
        Dim s As String
        Dim i As Integer

        i = r.Next(0, 26)
        s = "tile"
        If i <= 9 Then s += "0"
        s += i.ToString
        pictray1.Image = My.Resources.Tile02
        pictray1.Image = My.Resources.ResourceManager.GetObject(s)

        s = Application.StartupPath & "\tiles\" & s & ".jpg"

        pictray1.ImageLocation = s
    End Sub

ImageLocation property uses an URL to an image. When loading an image file from your computer, use PictureBox1.Image = Image.FromFile(<path to image>) instead, i.e. pictray1.Image = Image.FromFile(s) in your code.

You could also set a breakpoint at s = Application.StartupPath & "\tiles\" & s & ".jpg" line to check which file name you're trying to use. Or display the value of s variable in a message box after that line. The red x box comes because the image is not found.

And one more thing. You should use & to concatenate strings instead of +.

This article has been dead for over six months. Start a new discussion instead.