Home > database >  Randomize what picturebox the image will go into
Randomize what picturebox the image will go into

Time:10-23

I am very new to visual basics and was wondering how to put an image into a randomized picture box. I have 4 picture boxes and one image. When you click the button "Jouer" I want the image to appear in the picture box that the computer has chosen. Thanks in advance. This is the code I've tried so far:

   Private Sub btnJouer_Click(sender As Object, e As EventArgs) Handles btnJouer.Click
    T(0) = PictureBox1
    T(1) = PictureBox2
    T(2) = PictureBox3
    T(3) = PictureBox4

    lbl1.Text = lbl1.Text - 1

    Dim rand As New Random()
    For i = 0 To 3
        Randomize()
        Image = My.Resources.mario1
        Trouver = Image.ToString
        Trouver = rand.Next(0, T.Count)
    Next

  End Sub

CodePudding user response:

With some hard-coding of four PictureBoxes, it can be done simply with a Case statement

Dim rand As New Random()
Dim image = My.Resources.mario1
Dim i = rand.Next(4) ' 0, 1, 2, 3
Select Case i
    Case 0
        PictureBox1.Image = image
    Case 1
        PictureBox2.Image = image
    Case 2
        PictureBox3.Image = image
    Case 3
        PictureBox4.Image = image
End Select

Randomize is meant to be used with the old-style vb.net Rnd function, and the Random class doesn't require it.

Without hard-coding four PictureBoxes, you can specify the number of PictureBoxes and the prefix in the name

Dim rand As New Random()
Dim image = My.Resources.mario1
Dim i = rand.Next(4) ' 0, 1, 2, 3

Dim numberOfPictureBoxes = 4
Dim pictureBoxPrefix = "PictureBox"

Dim myPictureBox = DirectCast(Me.Controls.Find($"{pictureBoxPrefix}{i   1}", True).Single(), PictureBox)
myPictureBox.Image = image

Of course, you may want to clear all the PictureBoxes before setting one, so they are all cleared. You can use Me.Controls, but the PictureBoxes must be in the form, not in a different control such as a Panel or GroupBox. If that's the case use that container instead: container.Controls.Cast(Of Control...

Dim myPictureBoxes = Me.Controls.Cast(Of Control).Where(Function(c) c.Name.Contains(pictureBoxPrefix)).Cast(Of PictureBox)()

For Each pb In myPictureBoxes
    pb.Image = Nothing
Next

CodePudding user response:

It is usually best to declare Random as a class level variable. You then reuse the same instance each time you click the button. If you use a local variable and click the button quickly Random may use the same seed which is based on the system clock.

I also made the array of picture boxes a class level variable so I didn't have to rebuild it each time the button is clicked. I initialized the array in the Form.Load so we have already created PictureBoxes to fill it.

When you turn on Option Strict, you will see it is unwise to try to do arithmetic with Strings. Also assigning an Integer to a Text property (Text properties are String) is not correct.

I cleared all the picture boxes then got the index from the Random instance. I see you realize that the Next method parameters are inclusive of the first parameter and exclusive of the second parameter.

Assuming that the image has been assigned to the Resources correctly, we just need to assign it to the randomly selected PictureBox in the array.

Private Rand As New Random()
Private PBs() As PictureBox

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    PBs = {PictureBox1, PictureBox2, PictureBox3}
End Sub

Private Sub btnJouer_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Label1.Text = (CInt(Label1.Text) - 1).ToString
    For Each pb In PBs
        pb.Image = Nothing
    Next
    Dim index = Rand.Next(0, PBs.Count)
    PBs(index).Image = My.Resources.compiles_ship_it
End Sub

I changed a few control names and the name of resource to match my test program. I think you will be able to follow it anyway.

  • Related