Sahib
Sahib

Reputation: 1

Random image stuck on one image

I have code for a randomizer that puts a random image into 2 picture boxes but on one of them the picture stays the same and does not change. Here is my code.

Dim Random As Integer
Dim Random2 As Integer

Random = CInt(Math.Ceiling(Rnd() * 6)) + 0
Random = CInt(Math.Ceiling(Rnd() * 6)) + 0

If Random = 1 Then
    PictureBox1.Image = Image.FromFile("C:\Users\sahib\Desktop\another rounders -_-\another rounders -_-\bin\Dice Faces\Dice1.png")
ElseIf Random = 2 Then
    PictureBox1.Image = Image.FromFile("C:\Users\sahib\Desktop\another rounders -_-\another rounders -_-\bin\Dice Faces\Dice2.png")

I did this up to six then started again but this time with Random2 As Integer and PictureBox2 (the one that doesn't change image). I am very confused why this is happening.

Upvotes: 0

Views: 62

Answers (1)

Mary
Mary

Reputation: 15091

Big EDIT thanks to @Jimi.

First use the .net Random class. It is easier to use. Then in the Solution Explorer, add a new folder named Images. Then right click and add the images to the folder. You need to select all the files you added and the choose a Build Action -> Additional Files and Copy to Output Directory -> Copy if newer.

'Form level (class scope) can be seen by all methods
 Private Dice As New List(Of String)
 Private Rand As New Random

'Fill the Dice list just once in Form.Load
 Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For i = 1 To 6
        Dice.Add($"Images\Dice{i}.png")
    Next
End Sub

Private Sub RollDice()
    PictureBox1.Image = Image.FromFile(Dice(Rand.Next(1, 7)))
    PictureBox2.Image = Image.FromFile(Dice(Rand.Next(1, 7)))
End Sub   

Upvotes: 1

Related Questions