随机骰子图片

Random dice pictures

Picturebox 中骰子的 6 个面随机化的代码是什么?如果我单击滚动按钮,6 个图片框将随机化。使用定时器。计时器中的代码是什么。请帮忙。我是初学者,我不知道如何随机图片框。谢谢。

在进入代码之前,您需要执行一些步骤,我将在图片中向您展示这些步骤,这样您会更容易。

1.Add 'Resources' 选项卡中所有骰子面的图像。

2.The 设计:

在您的设计中添加一个计时器 (Timer1)、一个按钮 (Button1)、一个图片框 (Picturebox1)。

3.The代码:

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