USE A STRING AS OBJECT NAME

jacob rasel

Total Post:88

Points:616
Posted by  jacob rasel
VB.Net 
 1117  View(s)
Ratings:
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. AVADHESH PATEL

    Post:604

    Points:4228
    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!