我有這樣的代碼爲puzzle8項目:什麼是使我的puzzel 8項目更好的最佳途徑?
Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
Dim indexrand As Integer
Dim pos As Integer
Dim m As Integer
Dim j As Integer
For j = 0 To 100
pos = 0
indexrand = R.Next(0, 9)
For i = indexrand To 8
array.Insert(0, array(i + pos))
pos += 1
Next
m = 9 - indexrand
Do While m > 0
array.RemoveAt(array.Count - 1)
m -= 1
Loop
Next
我做了一個循環,隨機播放他們它完美地工作。然後,我希望它給每一個按鈕的值,所以我這樣做:
Button1.Text = array(0)
Button2.Text = array(1)
Button3.Text = array(2)
Button4.Text = array(3)
Button5.Text = array(4)
Button6.Text = array(5)
Button7.Text = array(6)
Button8.Text = array(7)
Button9.Text = array(8)`
不過,我在這裏有一個問題,因爲值9,不會變成「」,所以我這樣做:
If Button1.Text = 9 Then
Button1.Text = ""
ElseIf Button2.Text = 9 Then
Button2.Text = ""
ElseIf Button3.Text = 9 Then
Button3.Text = ""
ElseIf Button4.Text = 9 Then
Button4.Text = ""
ElseIf Button5.Text = 9 Then
Button5.Text = ""
ElseIf Button6.Text = 9 Then
Button6.Text = ""
ElseIf Button7.Text = 9 Then
Button7.Text = ""
ElseIf Button8.Text = 9 Then
Button8.Text = ""
ElseIf Button9.Text = 9 Then
Button9.Text = ""
End If `
這是行不通的。但是,我的問題是這是很多If
聲明。有什麼辦法可以循環使用,而不用輸入所有這些ifs?
您可以循環遍歷窗體上的所有按鈕與'爲每個Ctrl作爲控制在控件'。然後檢查'TypeOf ctrl是否是按鈕'。那麼你只需要做你的代碼。 – nbadaud
@nbadaud這是一個很好的建議,但你應該進一步擴展,因爲這個代碼是由一個不應該改變的按鈕觸發的。 –
對不起,因爲我之前說我是vb.net的新手,所以你想告訴我的不是那麼清楚。無論如何感謝您的答案。 – Kamal