此程序的目的是運行一系列的picturebox並將它的.image屬性設置爲特定的圖像。我不斷收到錯誤「對象引用未設置爲對象的實例」。來自顯示「DirectCast(Me.Controls(圖片(i)),圖片框).Image = My.Resources.glass_buttonred」....的行顯示奇怪的是,如果我將該代碼移動到for循環之外精細。未將對象引用設置爲對象的實例
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim pic(2) As Object
For i = 0 To 2
pic(i) = "picturebox" + Convert.ToString(i)
DirectCast(Me.Controls(pic(i)), PictureBox).Image = My.Resources.glass_buttonred
Next
Label1.Text = pic(1)
End Sub
這裏是工作代碼。謝謝!希望這將幫助其他人想將字符串轉換爲控制對象
Dim pic(2) As Object
For i = 0 To 2
pic(i) = "picturebox" + Convert.ToString(i + 1)
DirectCast(Me.Controls(pic(i)), PictureBox).Image = My.Resources.glass_buttonred
Next
Label1.Text = pic(1)
什麼是精確的,圖片框的實際名稱和你有多少? – Plutonix
名稱只是圖片框和3 .. – user2824324