Use a String As Object Name

Posted by  jacob rasel
 1664  View(s)
Rate this:
Hi Developers, 

How can I change a picture box's image when I only have a string that has the name of the picture box. (it probably doesn't really matter that it's a picture box or that I am changing it's image)

For example:

"picturebox1".image = myimage     

how would I get this to actually set an image for picturebox1?

Any help on above is really appreciated.
  1. Re: Use a String As Object Name

    Hi Everyone,

    Used CType in place of DirectCast and write following line of code

    Dim pb As PictureBox = DirectCast(Me.Controls.Find("picturebox1", True).FirstOrDefault(),  PictureBox)
    If pb IsNot Nothing Then
        pb.Image = myimage
    End If

      Modified On Mar-30-2018 02:14:13 AM