Results 1 to 6 of 6

Thread: RESOLVED [VB2010] Set a image depending from a String value

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    459

    RESOLVED [VB2010] Set a image depending from a String value

    I have about 100 little images (A1, A2,..., B1, B2,....) in Project/Resources

    My goal is to set a PicturBox.Image depending from the value of a MyImage as String variable.

    Obviously I can do:

    Code:
    Select Case MyImage
    
    Case "A1"
      PictureBox.Image = My.Resources.A1
    
    Case "A2"
      PictureBox.Image = My.Resources.A2
    
    ...................................
    but this mean to make a Select Case of 100 items....

    Question: is there a way to set directly the image from MyString, something like:

    PictureBox.Image = My.Resources.MyString ??

    Ty
    Last edited by phil2000; Aug 30th, 2014 at 11:26 AM.

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: [VB2010] Set a image depending from a String value

    I haven't tried it, but a quick Google search suggests something like this should work:

    Code:
    My.Resources.ResourceManager.GetObject(MyImage)

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: [VB2010] Set a image depending from a String value

    try this:

    Code:
    PictureBox1.Image = DirectCast(My.Resources.ResourceManager.GetObject(MyImage), Bitmap)

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: [VB2010] Set a image depending from a String value

    @paulg4ije...

    you nearly beat me there

    My.Resources.ResourceManager.GetObject(MyImage) returns an object and needs to be cast to either bitmap or image

  5. #5
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: [VB2010] Set a image depending from a String value

    Quote Originally Posted by .paul. View Post
    @paulg4ije...

    you nearly beat me there

    My.Resources.ResourceManager.GetObject(MyImage) returns an object and needs to be cast to either bitmap or image
    Sounds good to me

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2009
    Posts
    459

    Re: [VB2010] Set a image depending from a String value

    Quote Originally Posted by .paul. View Post
    try this:
    Code:
    PictureBox1.Image = DirectCast(My.Resources.ResourceManager.GetObject(MyImage), Bitmap)
    It works perfectly!!

    Ty

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width