我的代码中出现了ArgumentOutOfRangeException。我正在尝试制作基本的记忆瓷砖游戏。我目前正与合作伙伴合作,我们似乎无法弄清楚为什么我们会遇到这个例外。我的伴侣和我是imageLists的新手,我们并不完全确定我们是否正确使用它。他和我已经在互联网上搜索了如何正确使用图像列表的资源,并没有让我们知道我们做错了什么。然而,事实证明,我们显然没有做正确的事情。
我想把你的注意力转移到picBox_Click子程序,这是抛出异常的地方。线条类似于: sender.Image = imgList.Images.Item(intArray(i)) 是什么原因导致抛出ArgumentOutOfRangeExceptions。
问题是:我们如何正确索引imageList集合,以便我们可以比较两个图像并确定它们是不同还是相同?
非常感谢您的帮助。
Public Class Form1
' Global Variable Declarations:
' Image array that references the 16 picture resources.
Dim imgList As New ImageList()
' Integer array with 16 elements (0-15).
Dim intArray() As Integer = New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
' The last PictureBox Clicked
Dim lastIndex As Integer = 0
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)
imgList.Images.Add("america", My.Resources.america)
imgList.Images.Add("america", My.Resources.america)
imgList.Images.Add("canada", My.Resources.canada)
imgList.Images.Add("canada", My.Resources.canada)
imgList.Images.Add("italy", My.Resources.italy)
imgList.Images.Add("italy", My.Resources.italy)
imgList.Images.Add("egypt", My.Resources.egypt)
imgList.Images.Add("egypt", My.Resources.egypt)
imgList.Images.Add("russia", My.Resources.russia)
imgList.Images.Add("russia", My.Resources.russia)
imgList.Images.Add("china", My.Resources.china)
imgList.Images.Add("china", My.Resources.china)
imgList.Images.Add("united kingdom", My.Resources.united_kingdom)
imgList.Images.Add("united kingdom", My.Resources.united_kingdom)
imgList.Images.Add("japan", My.Resources.japan)
imgList.Images.Add("japan", My.Resources.japan)
End Sub
Sub RandomizeArray()
Dim temp As Integer
Dim j As Integer
Randomize()
For i As Integer = LBound(intArray) To UBound(intArray)
j = CInt(((UBound(intArray) - i) * Rnd()) + i)
temp = intArray(i)
intArray(i) = intArray(ninja)
intArray(ninja) = temp
Next i
End Sub
Private Sub picBox_Click(sender As System.Object, e As System.EventArgs) Handles picBox1.Click, _
picBox2.Click, picBox3.Click, picBox4.Click, picBox5.Click, picBox6.Click, picBox7.Click, _
picBox8.Click, picBox9.Click, picBox10.Click, picBox11.Click, picBox12.Click, picBox13.Click, _
picBox14.Click, picBox15.Click, picBox16.Click
Dim picBox() As PictureBox = New PictureBox() {picBox1, picBox2, picBox3, picBox4, picBox5, _
picBox6, picBox7, picBox8, picBox9, picBox10, _
picBox11, picBox12, picBox13, picBox14, picBox15, _
picBox16}
Dim i As Integer = FindSenderIndex(sender, picBox)
If lastIndex = 0 Then
sender.Image = imgList.Images.Item(intArray(i))
sender.Enabled = False
lastIndex = i
'Timer starts here
ElseIf imgList.Images.Keys(intArray(lastIndex)).ToString = imgList.Images.Keys(intArray(i)).ToString Then
sender.Image = imgList.Images.Item(intArray(i))
sender.Enabled = False
lastIndex = 0
'Timer ends here
Else
resetRecent(sender, picBox)
picBox(lastIndex).Enabled = True
lastIndex = 0
'Adds an extra point to fail
End If
End Sub