Friedpanseller
Friedpanseller

Reputation: 699

Use String to find my.resources

I have a number of resources called

My.Resources.diceDark1
My.Resources.diceDark2
My.Resources.diceDark3...

I want to do something like

For i as integer = 1 to 10
    button1.backgroundimage = My.Resources.diceDark i
Next

So it will cycle through all the resources and change the background image to that

Upvotes: 1

Views: 1689

Answers (1)

The Designer creates property getters and setters for the images etc you add to Resources. So, for an image named dicedark1.jpg, it creates:

Friend ReadOnly Property diceDark1() As System.Drawing.Bitmap
    Get
        Dim obj As Object = ResourceManager.GetObject("diceDark1", resourceCulture)
        Return CType(obj,System.Drawing.Bitmap)
    End Get
End Property

You can see these in Resources.Designer.vb. So the resource "names" you use are not something like variables, but Property names for the Resources object. But what you can do, is what you see in the getter, which is use GetObject:

Private DiceNames As String() = {"diceDark1", "diceDark2", "diceDark3" ...}
...
' assuming you have control refs in an array also:
For i As Int32 = 0 To 6
    picBox(i).BackgroundImage = My.Resources.ResourceManager.GetObject(DiceNames(i)) 
Next i

The property wrappers obviously make it easier to get at your resources. To use the loop, you'll need the target controls in an array or list since picBox + 1 or any variation thereof wont work any better than the My.Resources.DiceDark i reference.

Upvotes: 3

Related Questions