Search code examples
randomtimerpictureboxvb.net-2010

Random dice pictures


What is the code for randomizing the 6 faces of dice in the Picturebox. If I click the roll button the 6 pictureboxes will be randomize. Using the timer. What is the code in the timer. Please help. I'm a beginner and I don't know how to random pictureboxes. Thank you.


Solution

  • Before getting into the code, you need to do some steps, and i will show you these steps within pictures so it will be easier on you.

    1.Add all the faces of the dice as images in the 'Resources' tab.

    PIC-1

    PIC-2

    2.The design :

    Add to your design a timer (Timer1), a button (Button1), a picturebox (Picturebox1).

    3.The code :

    Public Class Form1
        Dim Rnd As New Random
        Dim FaceX, X As Integer
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer1.Stop()
            If X = 10 Then 'Note : 10 is the number of shuffling the images.
                X = 0
            Else
                FaceX = Rnd.Next(1, 7)
                If FaceX = 1 Then
                    PictureBox1.Image = My.Resources.DiceFace_1  'Note : replace [DiceFace_1] with the name of the image of the face1 of the dice that you added it in the Resources, and do that to all the coming commands....
                ElseIf FaceX = 2 Then
                    PictureBox1.Image = My.Resources.DiceFace_2
                ElseIf FaceX = 3 Then
                    PictureBox1.Image = My.Resources.DiceFace_3
                ElseIf FaceX = 4 Then
                    PictureBox1.Image = My.Resources.DiceFace_4
                ElseIf FaceX = 5 Then
                    PictureBox1.Image = My.Resources.DiceFace_5
                ElseIf FaceX = 6 Then
                    PictureBox1.Image = My.Resources.DiceFace_6
                End If
                X += 1
                Timer1.Start()
            End If
        End Sub
    End Class