2017-03-03 180 views
1

在Picturebox中對骰子的6個面進行隨機化的代碼是什麼?如果我點擊滾動按鈕,6個圖片框將隨機化。使用計時器。定時器中的代碼是什麼?請幫忙。我是一個初學者,我不知道如何隨機的圖片框。謝謝。隨機骰子圖片

回答

0

在進入代碼之前,您需要執行一些步驟,並且我會在圖片中向您顯示這些步驟,以便您更輕鬆地進行操作。

1.將骰子的所有面添加爲「資源」選項卡中的圖像。

PIC-1

PIC-2

2.設計:

添加到您設計的定時器(定時器),一個按鈕(按鈕1),一個圖片(Picturebox1)。

3,代碼:

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 
+0

謝謝主席先生。我懂了。它的工作。 –

+0

不客氣:) –