1

我正在尝试在 Visual Basic 中使用 Slots Machine 类型的程序,我已经检查了基本的插槽机制并使用

Private Sub roll_Click(sender As Object, e As EventArgs) Handles roll.Click
    a.Text = CStr(Int(Rnd() * 10))
    b.Text = CStr(Int(Rnd() * 10))
    c.Text = CStr(Int(Rnd() * 10))

    If a.Text = 7 And b.Text = 7 And c.Text = 7 Then
        MessageBox.Show("WINNER!")
    Else
        MessageBox.Show("Sorry, you are not a winner.")
    End If
End Sub

但我想在辅助文本标签中引用此单击事件。例如,如果单击按钮,它会跟踪您单击了多少次。就像我在想:

Private Sub amountlabel_Click(sender As Object, e As EventArgs) Handles amountlabel.Click
    for roll.Click do

        amount.Text = + 1
    End For
End Sub

谁能帮忙谢谢!

4

1 回答 1

1

只需在您的表单类中放置一个计数器变量 ( Integer),每次单击按钮时,只需增加计数器并更新标签的文本,如下所示:

Public Class Form1 Inherits Form
    Dim counter As Integer = 0

    Private Sub roll_Click(sender As Object, e As EventArgs) Handles roll.Click
        a.Text = CStr(Int(Rnd() * 10))
        b.Text = CStr(Int(Rnd() * 10))
        c.Text = CStr(Int(Rnd() * 10))

       If a.Text = 7 And b.Text = 7 And c.Text = 7 Then
           MessageBox.Show("WINNER!")
       Else
           MessageBox.Show("Sorry, you are not a winner.")
       End If

       ' Increment counter
       counter = counter + 1

       ' Update display
       amount.Text = counter.ToString()
    End Sub
End Class
于 2013-10-23T20:32:24.013 回答