跟踪点击的问题

时间:2013-10-23 20:07:09

标签: vb.net

我正在尝试使用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

任何人都可以帮助谢谢!

1 个答案:

答案 0 :(得分: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