我正在创建一个纸牌游戏,其中图像从图像列表加载并显示很短的时间,然后“隐藏” - 卡中的图像发生变化。当我点击这些图像中的任何一个时,我需要帮助,这样它会告诉我卡片的索引,然后我可以更改该卡片中的图像。这将由下面发布的代码中的最后一个Sub使用; “卡片用户点击”。
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
答案 0 :(得分:2)
您不应该尝试获取所点击卡片的索引,而应该将参考文献存储到图片框本身。添加另一个变量:
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.
然后在Click
事件处理程序中将ClickedCard
的值设置为引发事件的图片框(也就是事件的sender
)。
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
最后,只需使用该引用,而不是尝试从列表中获取它:
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
您还必须更新ClearGame()
方法,以便将ClickedCard
设置为空:
Private Sub ClearGame()
...the rest of your code...
ClickedCard = Nothing
End Sub