0

In this game I am creating, I have a set of "cards" as pictureboxes set to a random lot of images. When the game starts, the images in the pictureboxes are hidden and the user has to guess which image was in each card. I am having trouble with finding if the user's guess matches what was in the actual card. In the code below, I am using a listbox to score the names of images in which the user can guess from. https://i.stack.imgur.com/KFulk.jpg

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
robot14423
  • 33
  • 6
  • Okay, so there is no error. And .tag is storing the index of the image in the picturebox from a imagelist. " PicCard.Image = imglist1.Images(randomnumber) PicCard.Tag = randomnumber" – robot14423 Feb 11 '18 at 00:40
  • The problem i am having is that if the user selects the right item from the listbox for the image in the card, it will add to the score, which is correct, but then the user can click the correct listbox image name multiple times and it will add to the score, so it doesnt check if they are all right just if they have been selected. – robot14423 Feb 11 '18 at 00:45
  • How would you do that? – robot14423 Feb 11 '18 at 00:50
  • The listbox stores what is in the imagelist, which is in the same order. There is no need for Option Strict. You can see here: https://pastebin.com/XFXAxuKR – robot14423 Feb 11 '18 at 01:05
  • @robot14423 There is ALWAYS a need for Option Strict. – Mary Feb 11 '18 at 01:28
  • If I use "Option Strict" it will make it so I cannot convert strings to integers. – robot14423 Feb 11 '18 at 01:28
  • Let's see... TryParse, Parse ConvertTo, CInt and I am sure lots more. – Mary Feb 11 '18 at 01:31
  • I have code on my clipboard for you ready to paste as an answer but first you must promise to turn on Option Strict and keep it on for all your coding. By the way, you made a huge mistake with Plutonix. Don't turn down any more help from those with almost 35000 reps. (not me) – Mary Feb 11 '18 at 02:31
  • Okay Mary, I will start using Option Strict. Thank you for helping, i appreciate you – robot14423 Feb 11 '18 at 02:35

1 Answers1

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

The static list will persist between calls to this sub. You will have to clear this list when you go to a new round. I hope this helps with the problem you mentioned in your comments. :-)

Mary
  • 14,926
  • 3
  • 18
  • 27