2013-02-27 100 views
0

我有一個計時器,滾動數字1-6並着陸在隨機的一個。問題是我有一個picturebox,我需要做同樣的事情。我有骰子的圖像,但我不知道如何滾動所有的骰子圖像,以匹配我下面的代碼。我想讓他們兩人都跑,所以他們匹配。我完全卡住了!Picturebox骰子滾動(使圖片滾動看起來像骰子滾動)?

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
     m = m + 10 
     If m < 1000 Then 
      n = Int(1 + Rnd() * 6) 
      LblDice.Text = n 

     Else 
      Timer1.Enabled = False 
      m = 0 
     End If 


    End Sub 

    Private Sub RollDiceBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RollDiceBtn.Click 
     Timer1.Enabled = True 

     DisplayDie(die1PictureBox) 

    End Sub 
+1

請不要編輯了你的問題,關鍵的一部分是給別人看的,學習了。 – tcarvin 2013-04-16 19:00:19

回答

0

我不知道這是不是最好的解決辦法,但你可以把「滾動圖片」在一個ImageList與你想要的順序。然後用TimerTick中的計數器可以縮小imageList中的下一個圖像。

我發現這個代碼在我的電子郵件:

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick 
    If ticks < 11 Then 'I got 11 images 
     PictureBox1.BackgroundImage = ImageList1.Images(ticks) 
     ticks += 1 
    Else 
     ticks = 0 
     PictureBox1.BackgroundImage = ImageList1.Images(ticks) 
     ticks += 1 
    End If 
End Sub 

後評論: 您添加一個ImageList您Form.You正在爲您的圖像屬性的圖像列表(大小,深度)。在你的ImageList你爲你的骰子添加6張圖片。 然後你添加以下代碼:

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick 
    m = m + 1 
    If m < 10 Then 'How many ticks (rolls) do you like 
     n = Int(1 + Rnd() * 6) 
     Label1.Text = n 'The labels is showing your random number (dice number) 
     PictureBox1.Image = ImageList1.Images(n - 1) 'This is showing the dice image to the pictureBox:the same as the number 
     'I use n-1 because imageList is zero based. 
     'Important: my images are in the right order 1,2,3,4,5,6 in the imageList 
    Else 
     Timer1.Enabled = False 
     m = 0 
    End If 
End Sub 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    Timer1.Enabled = True 
End Sub 
+0

好的..所以如果我把這些代碼放在..我怎麼把6個不同的骰子放在一個圖像列表中?現在顯示我使用的圖片diePicture.Image = Image.FromFile(_ Directory.GetCurrentDirectory&「/ Images/die」&_ face&「.png」)我如何確保正確的圖片顯示當計時器停在一定數量時啓動嗎?謝謝 – Cliff 2013-02-27 13:36:50

+0

你的隨機號碼不對。每次啓動程序都是同一個序列,但這是一個不同的問題,而不是在這裏回答。 – Nianios 2013-02-27 14:06:26

+0

感謝您的幫助! – Cliff 2013-02-27 17:53:21