Tuesday 18 June 2019

arrays - Object reference not set to an instance of an object

The purpose of this program is to run through a range of pictureboxes and set it's .image property to a particular image. I keep getting the error "Object reference not set to an instance of an object". Coming from the line that shows "DirectCast(Me.Controls(pic(i)), PictureBox).Image = My.Resources.glass_buttonred"....Whats weird is if i move that code outside of the for loop it runs just fine.


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

HERE IS THE WORKING CODE. THANKS! Hope it will help others wanting to convert string to control object


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)

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...