好的,所以我对这段代码有两个问题:
我有8个图片框,我正在尝试对随机生成的数字进行检查,以查看它是否与图片框末尾的数字相匹配(如果随机数= 8,则为IE,另一个常量被放入图片框8而没有其他,但只有在它已经没有东西的情况下才会进入。)
我已经通过简单地将每个检查作为单个if语句运行来完成它但是......如果后面的语句是180,那么代码太多了。
我正在尝试(如你所见)通过几个while循环运行它。我遇到问题的主要部分是在阵列中为一个特定的图片框分配一个来自阵列的图像。我可以使用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
答案 0 :(得分:1)
我不确定您在这里尝试使用哪种语言。我的意思是,无论是VB.Net还是VB6类! 我假设它是VisualBasic.Net(VB.Net)。
我不确定我是否清楚地理解你。所以,我给你一个小例子代码供你试试。我相信其中的评论几乎可以解释。
首先创建一个新的空白项目。在其中添加5个文本框(无需更改其名称。只需拖放5个文本框)。添加一个按钮,然后双击它并粘贴我在下面显示的代码:
Dim rand As New Random '~~~ for creating the random numer
Dim intTotal As Integer = 5 '~~~ I have 5 textboxes with the names ending by number from 1 to 5
Dim intUpdated As Integer = 0 '~~~ to store the total number of updated textboxes
'~~~ first of all we are looping through the textboxes(am just showing how you can fetch them via it's name. There are other ways to iterate through the controls though!)
For i As Integer = 1 To intTotal
Dim tbox As TextBox = DirectCast(Me.Controls.Item("TextBox" & i.ToString()), TextBox) '~~~ here, we are fetching the TextBox via it's "Name" property, by appending the number at the end of "TextBox". Because in my form, I have "TextBox1", "TextBox2", "TextBox3", "TextBox4" and "TextBox5"
tbox.Tag = "untouched" '~~~ just setting it's "Tag" property to some text so that we could later use it evaluate whether we have touched this textbox via the random number
'-------------------------------------------------
'if you want to do something else like loading up default text or something, you can do it here too
'-------------------------------------------------
Next
'~~~ the randomly choosing part..
Dim j As Integer = 1
Do While intUpdated < intTotal '~~~ loop until we have taken into consideration all the textboxes via random number
Dim r As Integer = rand.Next(1, intTotal + 1) '~~~ create a random number between 1 and 5 (both inclusive)
'~~~ fetch that TextBox via it's name. Note that, am appending the random number to the name "TextBox". For eg, if the random number generated was "2", it would access "TextBox2"
Dim tbox As TextBox = DirectCast(Me.Controls.Item("TextBox" & r.ToString()), TextBox)
'~~~ since we have textbox now, we are checking whether we have accessed it earlier via random number
If tbox.Tag.ToString() = "untouched" Then '~~~ if not accessed before...
tbox.Tag = "touched" '~~~ mark the textbox as accessed via random number
intUpdated += 1 '~~~ increment the counter variable that we are using for storing the number of textboxes we accessed
'-------------------------------------------------
'~~~ do whatever you want on it here.. Right now, am just inserting some text on it to show the order in which the textboxes are accessed via our random number
tbox.Text = "updated" & j.ToString
j += 1
'-------------------------------------------------
End If
Loop
运行之后,我希望你能够得到这个想法。要访问位于表单中的控件名称,您可以像这样访问它:Me.Controls.Item("control_name_goes_here")
。因此,当您提及控件的名称时,可以将数字附加到字符串。我在上面的代码中使用过它。看看它。
&
,就像连接运算符一样!即,Dim a As String = "Akhilesh" & "B Chandran"
。这会将Akhilesh B Chandran
存储在该变量a
中。对于AND操作,您需要使用AND
。在C,C ++,C#等中它是&&
(注意有两个&符号)
另外,在VB.Net中,您应该使用MessageBox.Show()
而不是MsgBox()
函数。那是更好的方式。
希望这会有所帮助。