使用.tag匹配卡片

时间:2018-02-11 00:20:21

标签: vb.net winforms picturebox

在我正在创作的这个游戏中,我有一组“卡片”作为图片框设置为随机批量的图像。当游戏开始时,图片框中的图像被隐藏,并且用户必须猜测每张卡中的图像。我在查找用户的猜测是否与实际卡中的内容匹配时遇到问题。 在下面的代码中,我使用列表框来评分用户可以猜测的图像名称。 https://imgur.com/a/xCg8X

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged

    If ClickedCard Is Nothing Then 'Make sure that a card has been clicked, otherwise the below code will fail.
        MsgBox("You must select a card.")
        Return 'Do not continue execution of this code.
    End If
    btnSubmit.Visible = True
    ClickedCard.Image = imglist1.Images(ListBox1.SelectedIndex)


    If ClickedCard.Tag = ListBox1.SelectedIndex Then
        roundscore += 1
        If roundscore = Cards.Count Then
            MsgBox("All right")

        End If
    End If
 End Sub

1 个答案:

答案 0 :(得分:1)

 Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Static AlreadySelected As New List(Of Integer)

        If AlreadySelected.Contains(ListBox1.SelectedIndex) Then
                MessageBox.Show("Already select once")
                Exit Sub
            End If

        AlreadySelected.Add(ListBox1.SelectedIndex)
        'Your other code here
    End Sub

静态列表将在对此子组的调用之间保持不变。进入新一轮时,您必须清除此列表。我希望这有助于您在评论中提到的问题。 : - )