visual studio c# - dynamic insertion of images via reference

42 views Asked by At

I am getting more and more enthusiastic about programming with C# in Visual Studio and I am trying to finalize a quiz that I made, so that it's exportable.

I had a working version which referenced images dynamically to a static location, but I am struggling to adjust this to extract from the resources folder now. I hope someone can help me out.

  1. I have buttons on my form, which set the string categories to a certain category (countries, flags, music) and the integer imagecounter to 0.

  2. I have images named after the categories which then show up from that category when the quiz is started and cycle through it with the imagecounter.

pictureBox1.ImageLocation = "C:\\Users\\Thijs\\Desktop\\quiz-map\\" + categories + imagecounter + ".jpg";

imagecounter++;

This works perfectly fine, but sharing the solution with friends, I want it to pull the images from the resource file. I have added all images there, but am not able to cycle through the images in the same way:

pictureBox1.Image = Properties.Resources. + category + imagecounter;

How can I make this work?

I can't seem to make use of the same reference to cycle through the images and tried some different ways of notation, but can't seem to find the way to make this happen.

1

There are 1 answers

0
Thijsmans On

Thanks to Ralf's suggestions I managed to fix the above by using the following:

pictureBox1.Image = Properties.Resources.ResourceManager.GetObject(categories + imagecounter) as Image;

Thank you so much!