[英]Checking the index of a picturebox image
我正在創建一個紙牌游戲,其中從圖像列表加載圖像並顯示一小段時間,然后“隱藏” - 卡片中的圖像發生變化。 當我單擊這些圖像中的任何一個時,我需要幫助,以便它告訴我卡片的索引,然后我可以更改該卡片中的圖像。 這將由下面發布的代碼中的最后一個 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
您應該只存儲對圖片框本身的引用,而不是嘗試獲取被點擊卡片的索引。 添加另一個變量:
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
設置為 null:
Private Sub ClearGame()
...the rest of your code...
ClickedCard = Nothing
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.