Hello!
I got a neaty tricky situation here..

I have a picturebox1 and listbox1
and a few items in listbox1.
I got images in resources.resx (bmp) and want to load different images depending on what index is selected..

I wanted for it to be something like this..

Bitmap bmp = Properties.Resources.Image_ + listBox1.SelectedIndex;
            pictureBox1.Image = bmp;

as my images are named Image_0, Image_1, Image_2 and so on..
I wanted the number of the index to get to the Image_* so that I don't have to rewrite the code for each image.. how can I do that?

Well suppose that you ve added images to the Resources and you have them already in your Listbox. You can do this by writting the folowing code:

List<Image> list;
        public Form1()
        {
            InitializeComponent();
            list = new List<Image>();
            list.Add(Properties.Resources.image1name);
            list.Add(Properties.Resources.image2name);
            list.Add(Properties.Resources.image3name);
            //and so on...


        }

        private void Form1_Load(object sender, EventArgs e)
        {

            listBox1.DataSource = list;

        }

Now that you have your Image objects in your listbox by clicking on one of them you should be able to display your image to the PictureBox by using this code.

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

                pictureBox1.Image = (Image)listBox1.SelectedItem;

        }

So you just generate the event and put the folowing code inside, You have to cast the items in the listbox otherwise you ll get an error.

commented: good +15

that is one way to do it.. but.. feels stupid to explain this here when i could have done it on the first post..

I got these images in the resources.resx;
Image_0
Image_1
Image_2
BmpFile_0
BmpFile_1
BmpFile_2
BmpFile_3
BmpFile_4
ImageFile_0
ImageFile_1
...
...

I got 2 picturebox in my form which when I click on the 1st one I get 3 items in the listbox
and if i press on the other picture I get 5 items in the listbox

ofcourse I could do 3 or more different list's but I got around 50 different images with different amount of sort.. Feels like there should be an easier way to do than to add a new list for each image set

now i m really confused

I got 2 picturebox in my form which when I click on the 1st one I get 3 items in the listbox
i really don t understand this, what u are saying means that your image generates a list? In your first post you were talking about the inversive proccess were nt you? Please be more specific or post more code.

yeah.. sorry that was my stupidity taking over.. not sure what I actually wrote there myself.. Let me try to explain it more on this post..

I got 2 pictureboxes, when clicked on one or another it creates a list in a listbox

And by choosing an item on the listbox it should load an image from the resources.resx onto a 3rd picturebox
how is the best way to do this?

I tried this:

private void PictureBox1_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    listBox1.Items.Add("Classic Skin");
    listBox1.Items.Add("Second Skin");
    listBox1.SetSelected(0, true);
}

private void PictureBox2_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    listBox1.Items.Add("Classic Skin");
    listBox1.Items.Add("Second Skin");
    listBox1.Items.Add("Third Skin");
    listBox1.Items.Add("Fourth Skin");
    listBox1.Items.Add("Fifth Skin");
    listBox1.SetSelected(0, true);
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

    //Bitmap bmp = Properties.Resources.Image_0;

    /*if(listBox1.SelectedIndex == "0")
    {
        Bitmap bmp = Properties.Resource.Image_0;
    }*/

    Bitmap bmp = Properties.Resources.Image_ + listBox1.SelectedIndex; // Only loads bmp files that starts with Image_
    pictureBox3.Image = bmp;

}

As for now, the code in listBox1 is only for the picturebox1 which I wanted to tryout to load images depending on which selectedindex I choosed, has no interactions with picturebox2..

Images in picturebox1 are Image_*
Images in picturebox2 are BmpImage_*

and if would be possible..

to make the listbox interact with both pictureboxes about what image should load when selecting an item in listbox even thought they do not have the same starting name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.