2

I am creating a card game where images are loaded from a imagelist and displayed for a short amount of time, then "hidden" - the image in the card changes. I need help with when I click any one of these images so that it will tell me what index the card has so then I can change the image in that card. This will be used by the last Sub in the code posted below; "CARD THE USER CLICKED".

Public Class Form1
Private Cards As New List(Of PictureBox)
Private randomnumber As Integer
Private UserChoice As Integer
Private Timer As Integer

Private Sub SetupCards(numberofcards As Integer)
    ClearGame()
    For i As Integer = 0 To numberofcards
        Dim PicCard As PictureBox = New PictureBox()
        RandomCard()
        PicCard.Width = 100
        PicCard.Height = 200
        PicCard.Top = 50
        PicCard.Left = 50 + PicCard.Width * i
        Me.Controls.Add(PicCard)
        PicCard.Image = imglist1.Images(randomnumber)
        PicCard.Tag = randomnumber
        AddHandler PicCard.Click, AddressOf Me.cardflip_click
        Cards.Add(PicCard)
    Next i
End Sub

Private Sub ClearGame()
    If Cards.Count > 0 Then
        For i As Integer = 0 To Cards.Count - 1
            Me.Controls.Remove(Cards(i))
        Next
    End If
    ' Clear the cards if they were already setup from a previous game.
    Cards.Clear()
End Sub

Private Sub EndRound()
    'set all the images to back of card
    If Cards.Count > 0 Then
        For i As Integer = 0 To Cards.Count - 1
            Cards(i).Image = imglistBackOfCard.Images(2)
        Next
    End If

End Sub

Private Sub cardflip_click(sender As Object, e As EventArgs)
    Dim picture As PictureBox = CType(sender, PictureBox)
    Dim idTag As Integer = CType(picture.Tag, Integer)
    'MsgBox(idTag)
    UserChoice = idTag
End Sub

Private Sub btnstartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
    Dim howmanycards As String
    howmanycards = InputBox("How Many Cards?", "Please Enter")
    SetupCards(Int(howmanycards - 1))
    ListBox1.Enabled = True
    ListBox1.Visible = True
    For Each imagesNames As String In imglist1.Images.Keys
        ListBox1.Items.Add(imagesNames)
    Next
    Timer1.Start()
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    If UserChoice = ListBox1.SelectedIndex Then
        MsgBox("correct")
        Cards(CARDTHEUSERCLICKED).Image = imglist1.Images(UserChoice)
        ' Cards(idTag).Image = 
    Else
        MsgBox("WRONG :(")
        Cards(CARDTHEUSERCLICKED).Image = imglist1.Images(UserChoice)
    End If
End Sub
End Class
Robert Caden
  • 19
  • 11
robot14423
  • 33
  • 6

1 Answers1

2

Instead of trying to get the index of the clicked card, you ought to just store the reference to the picture box itself. Add another variable:

Private Cards As New List(Of PictureBox)
Private randomnumber As Integer
Private UserChoice As Integer
Private Timer As Integer
Private ClickedCard As PictureBox 'This is new.

Then in your Click event handler set the value of ClickedCard to the picture box that raised the event (aka the sender of the event).

Private Sub cardflip_click(sender As Object, e As EventArgs)
    Dim picture As PictureBox = CType(sender, PictureBox)
    Dim idTag As Integer = CType(picture.Tag, Integer)

    UserChoice = idTag
    ClickedCard = picture
End Sub

Finally, just use that reference instead of trying to get it from your list:

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

If UserChoice = ListBox1.SelectedIndex Then
    MsgBox("correct")
    ClickedCard.Image = imglist1.Images(UserChoice)
Else
    MsgBox("WRONG :(")
    ClickedCard.Image = imglist1.Images(UserChoice)
End If

You must also update your ClearGame() method so that it sets ClickedCard to null:

Private Sub ClearGame()
    ...the rest of your code...

    ClickedCard = Nothing
End Sub
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75