Click here to Skip to main content
14,929,999 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have a button named "Portugal", "United States", "Spain".

I have a flag image named Portugal.png, United States.png, Spain.png in my resources.

I want whenever I click the buttons my form (e.g Portugal button) the Picturebox1.Image turns into Portugal.png and whenever I click the United States button the PIcturebox1.image turns into United States.png

Pseudo code:

Private Sub ButtonClick
Picturebox1.image = My.Resources(button1.text & ".png")
End Sub

What I have tried:

My.Resources(Button1.Text & ".png").

*Doesn't work*
Posted
Updated 21-Sep-16 7:12am
Comments
Richard Deeming 21-Sep-16 13:01pm
   
Why have you tagged your question as "C#" when you're using VB.NET?
Daniel Santos 21-Sep-16 13:02pm
   
I can easily convert it to VB.NET and I'll maybe get some more replies I think...
Richard Deeming 21-Sep-16 13:05pm
   
It's already VB.NET, but you've added the "C#" tag to your question. It's nothing to do with C#! :)

VB.NET's "My.Resources" provides strongly-typed access to the resources in your project. It has a property for each file embedded in the resources, but it doesn't provide an indexer to access the files with a dynamic name.

However, it does provide a property called ResourceManager, which will let you access the resources by name:
Retrieving Resources with the ResourceManager Class[^]
VB.NET
Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim button As Button = DirectCast(sender, Button)
    Dim resource As Object = My.Resources.ResourceManager.GetObject(button.Text)
    Picturebox1.Image = DirectCast(resource, Image)
End Sub

NB: You only need the name of the resource, not the file extension.
   
Comments
Daniel Santos 21-Sep-16 14:24pm
   
@Richardd Deeming, I already figured it out, your answer is correct.
I only used this:
PictureBox2.BackgroundImage = My.Resources.ResourceManager.GetObject(Button1.Text)
this.pictureBox1.Image = global::RecordStock.Properties.Resources.CONFIG2;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900