2

I'm writing a program about trying different images in picturebox, but the image must correspond to the text. Plus it must come from "Resources" folder of the project.

this is what i want to do if the text "apple" displays to the screen then the image with a filename "apple" would also display in the picturebox..

I can do it in "if-else" like this

string word="apple";
if(word==apple)
pictureBox1.Image=  WindowsFormsApplication4.Properties.Resources.apple;

but What if I have a thousand images, I still think there's an easy way for this,..

i'm trying this one,,

string word=label1.Text;//label1.text changes from time to time
pictureBox1.Image= WindowsFormsApplication4.Properties.Resources.word;

but I know "word" is string....It's not possible...I cannot append string to the syntax....

2 Answers 2

5

You can pass in a string using the GetObject method of the ResourceManager class:

string itemName = label1.Text;
this.pictureBox1.Image = 
    (Image)Properties.Resources.ResourceManager.GetObject(itemName);
Sign up to request clarification or add additional context in comments.

2 Comments

THANKS IT WORKED!! does it go the same with shockwave??
@Soliven Shockwave? Not sure what that is.
1

If you open resources .Designer.cs file code , you will see something like:

internal static System.Drawing.Bitmap apple {
    get {
        object obj = ResourceManager.GetObject("apple", resourceCulture);
        return ((System.Drawing.Bitmap)(obj));
    }
}

so you can do the same thing:

string word=label1.Text;//label1.text changes from time to time
pictureBox1.Image= (System.Drawing.Bitmap)WindowsFormsApplication4
                                           .Properties
                                           .Resources
                                           .ResourceManager.GetObject(word);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.