在VB.NET中按下按钮的顺序

时间:2013-11-03 14:56:05

标签: vb.net winforms button sequence

我的WinForm中有多个按钮。选择随机序列,必须按顺序按下按钮。

如何检测序列是否被破坏,即按钮没有以正确的顺序点击?

关键是我想知道按下某个按钮后按下了哪个按钮

Dim clicked As Boolean = False 
Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click 
   clicked = True 
End Sub

3 个答案:

答案 0 :(得分:1)

使所有按钮触发相同的处理程序并将Text()属性追加到String:

Private sequence As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, _
    Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button0.Click

    Dim btn As Button = DirectCast(sender, Button)
    sequence = sequence & btn.Text

    ' ... possibly do something with "sequence" ...

End Sub

答案 1 :(得分:0)

试试这个......

您需要添加10个按钮(在本例中),并在标签属性中输入数字1到10 ......

编辑:更聪明的方式; - )

Dim lstButtonOrder As New List(Of Integer)
Dim lstMyRandomArray As New List(Of Integer)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    SetupArray()

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click,
                                                                        Button4.Click, Button5.Click, Button6.Click,
                                                                        Button7.Click, Button8.Click, Button9.Click, Button10.Click
    Dim butPressed As Button = TryCast(sender, Button)

    lstButtonOrder.Add(butPressed.Tag)

    If lstButtonOrder.Count = lstMyRandomArray.Count Then

        If Enumerable.SequenceEqual(lstButtonOrder, lstMyRandomArray) = True Then

            MsgBox("Well Done!")

        Else

            MsgBox("You Fail")

        End If

        SetupArray()

    End If
End Sub

Private Sub SetupArray()
    Dim rnd = New Random
    lstMyRandomArray = Enumerable.Range(1, 10).OrderBy(Function(n) rnd.Next).ToList

    lblOrder.Text = "Please hit the buttons in this order..." & vbCrLf

    lstMyRandomArray.ToList().ForEach(Sub(x)
                                          lblOrder.Text &= x.ToString & " "
                                      End Sub)

    lstButtonOrder.Clear()

End Sub

当然,我可以在这里解析名称,但我更喜欢使用标签属性......

答案 2 :(得分:0)

我会保留一系列按钮:

Private buttons() As Button = { Me.Button1, Me.Button2, Me.Button3, … }

你可以做一个处理程序:

Private Sub Button_Click(sender As Object, e As EventArgs)
    ' TODO
End Sub

将处理程序添加到New()

中的按钮
Public Sub New()
    Me.InitializeComponent()

    For Each b In buttons
        AddHandler b.Click, AddressOf Button_Click
    Next
End Sub

在执行此操作之后(或之前),随机化buttons。然后跟踪您希望用户单击的下一个:

Private currentButton As Integer = 0

在处理程序中,检查:

If sender Is buttons(currentButton) Then
    ' Move to the next button
    currentButton += 1

    ' Was that the end of them?
    If currentButton = buttons.Length Then
        ' All buttons were pressed in the right order
    End If
Else
    ' Wrong!
End If

您可以使用列表,但如果您知道它们与您已有的订单相匹配,则无需跟踪按下的每个按钮。