เอาล่ะ ฉันมีปัญหาสองประการกับรหัสนี้:
ฉันมีกล่องรูปภาพ 8 กล่อง ฉันกำลังพยายามตรวจสอบตัวเลขที่สร้างแบบสุ่ม เพื่อดูว่าตรงกับตัวเลขที่อยู่ท้ายกล่องรูปภาพหรือไม่ (เช่น ถ้าตัวเลขสุ่ม = 8 ค่าคงที่อื่นจะถูกใส่ลงในกล่องรูปภาพ) 8 และไม่มีอย่างอื่น แต่ถ้ายังไม่มีบางอย่างอยู่ในนั้น)
ฉันทำมันโดยเพียงแค่ใช้เช็คทุกครั้งเป็นคำสั่ง if เดียว แต่... 180 ถ้าคำสั่งในภายหลัง... นั่นมากเกินไปสำหรับการเขียนโค้ด
ฉันกำลังพยายาม (อย่างที่คุณเห็น) รันมันผ่านหลาย ๆ ลูป ส่วนหลักที่ฉันมีปัญหาคือการกำหนดกล่องรูปภาพเฉพาะในอาเรย์ด้วยรูปภาพจากอาเรย์ ฉันสามารถรับรูปภาพโดยใช้
imgName
และimgPictures(i)
โดยใช้ในรูปแบบPictureBox1.Image = imgPictures(i)
แต่ไม่สามารถแทนที่PictureBox1.Image
ด้วยpicBoxes().Image
ได้การใช้ MsgBoxes ที่ฉันทิ้งไว้ในโค้ด ลูปและคำสั่ง if เรียกใช้ "Debug1" และ "Debug2" แต่ไม่มีคำสั่งอื่นๆ... ทำไมจึงเป็นเช่นนี้
ฉันจะทิ้งรหัสไว้และดูว่าพวกคุณจะทำอะไรได้บ้าง
Dim i As Integer = 1
Dim x As Integer = 1
Dim rndnumber As Integer = mathsclass.get_randomnumber()
Dim imgPictures(20) As Image
Dim picBoxes(8) As PictureBox
picBoxes = New PictureBox() {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8}
Dim imgName As String = ("_" & i)
imgPictures(0) = My.Resources.ResourceManager.GetObject(imgName)
picBoxes(x).Image = imgPictures(i)
While (i <= 20)
MsgBox("Debug1")
rndnumber = mathsclass.get_randomnumber()
imgName = ("_" & i)
imgPictures(i) = My.Resources.ResourceManager.GetObject(imgName)
x = 0
While (x < 8)
MsgBox("Debug2")
If ((randomnumber = i) & (randomposition = x)) Then
MsgBox("Debug3")
picBoxes(x).Image = imgPictures(i)
Else
While (rndnumber = randomnumber)
MsgBox("Debug4")
rndnumber = mathsclass.get_randomnumber()
End While
MsgBox("Debug5")
If ((randomnumber <> rndnumber) & (randomposition <> x)) Then
MsgBox("Debug6")
imgName = ("_" & rndnumber)
imgPictures(i) = My.Resources.ResourceManager.GetObject(imgName)
picBoxes(x).Image = imgPictures(i)
End If
MsgBox("Debug7")
End If
MsgBox("Debug8")
x += 1
End While
MsgBox("Debug9")
i += 1
End While