0

我有一个计时器,它在数字 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
4

1 回答 1

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 添加到您的表单中。您正在为 imageList(size, depth) 中的图像设置属性。在您的 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
于 2013-02-27T08:01:02.033 回答