该程序的目的是运行一系列图片框并将其 .image 属性设置为特定图像。我不断收到错误“对象引用未设置为对象的实例”。来自显示“DirectCast(Me.Controls(pic(i)), PictureBox).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)