我有一个计时器,它滚动数字1-6并随机登陆。问题是我有一个我需要做同样的图片框。我有骰子图像但我无法弄清楚如何滚动所有骰子图像以匹配我下面的代码。我希望他们两个都跑,所以他们匹配。我完全陷入困境!!
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
答案 0 :(得分: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(大小,深度)中设置图像的属性。在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