In my image resources I have pictures with following names: c1.png, c2.png, c3.png, ...
Whereas referring to the first image works with My.Resources.c1
I would like to display them in the cycle like this
For i = 1 To numberOfPictures
Dim tmpPicture As New PictureBox
tmpPicture.Image = cstr(My.Resources.c) & cstr(i) & ".png"
next
Now this of course doesn't work, because string cannot be converted to System.Drawing.Image. Any ideas how to solve this? I know it's really easy in VB where I did it like this:
For i = 1 To numberOfPictures
imgName.Picture = loadPicture(ThisWorkbook.Path & "\picturesfolder\" & CStr(i) & ".png")
next i
Thanks to Steven's terrific answer I was able to figure out the C# usage
although my image was an icon and actual code that worked was
Please up vote Stevens answer(they wont let me yet). That kind of answer is helpful to those who desperately need a quick answer AND for those who want to learn coding.
You have to put the images you'd like to cycle into a collection, then you can do a for loop on the collection.
It will look something like this:
You can define the collection globally and add the images to it in the constructor.
You could also have a look on the My.Resources.ResourceManager. Currently i'm not sure if it provides the desired functionality...
Edit: Well, see Steven Doggart post for that answer.
Try this:
It's pretty self-explanatory, but basically, you're just using the
GetObject
method of theResourceManager
singleton instance to return the resource you specify dynamically at runtime rather than referencing them how you would normally by their static references (e.g.My.Resources.c1
) at design time.Yes, you can do that like this:
Here's a hint. Sometimes it's very useful to look at the designer code. If you right-click on
My.Resources.c1
and choose the Go to Definition option, you'll see the code that is actually executed when you access that property. Thec1
property is, obviously, not built in as part of the .NET framework. It's an auto-generated property in a Designer file. The Resources designer screen automatically generates that code for you.