25

I have a group of images in my My.Resources. I want to select select images to display dynamically at run time. How do I do this?

'Static (Compile time) Assignment
UltraPictureBox1.Image = my.Resources.zoo_picture_1

'Dynamic (Runtime) Assignment
UltraPictureBox1.Image = ???
Jeff
  • 8,020
  • 34
  • 99
  • 157

5 Answers5

43

Found the solution:

UltraPictureBox1.Image = _
    My.Resources.ResourceManager.GetObject(object_name_as_string)
Eduardo Molteni
  • 38,786
  • 23
  • 141
  • 206
Jeff
  • 8,020
  • 34
  • 99
  • 157
  • 2
    Oh man they could have made the same code work in both situations! I mean, .NET is full of those things like "do not say it's "form1", call it "me" instead". They could simply have auto-correction or even better, compile the code in the same way. /rant – Camilo Martin Dec 10 '09 at 11:16
14

This works for me at runtime too:

UltraPictureBox1.Image = My.Resources.MyPicture

No strings involved and if I change the name it is automatically updated by refactoring.

Andrea Antonangeli
  • 1,242
  • 1
  • 21
  • 32
7

Make sure you don't include extension of the resource, nor path to it. It's only the resource file name.

PictureBoxName.Image = My.Resources.ResourceManager.GetObject("object_name") 
2
Dim resources As Object = My.Resources.ResourceManager
PictureBoxName.Image = resources.GetObject("Company_Logo")
Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
2

Sometimes you must change the name (or check to get it automatically from compiler).

Example:

Filename = amp2-rot.png

It is not working as:

PictureBoxName.Image = resources.GetObject("amp2-rot.png")

It works, just as amp2_rot for me:

 PictureBox_L1.Image = My.Resources.Resource.amp2_rot
PeterN
  • 217
  • 1
  • 4
  • 15