making the image on a picturebox change when a specific name is selected from a listbox in C#

StackOverflow https://stackoverflow.com/questions/21870855

  •  13-10-2022
  •  | 
  •  

Вопрос

This is what I've found so far because my book doesn't have anything on PictureBoxes besides two little notes at the bottom of the page. The image in the box is ssupposed to be nothing in the beginning but as the user selects an item from lstBxBagType the image in the picture box should change into that specific item.

private void lstBxBagType_SelectedIndexChanged(object sender, EventArgs e)
{

    if (this.lstBxBagType.Text == "Beaded-$45.00")
        pictureBox1.Image = new Bitmap(@"C:\Users\me\Desktop\Ch10As\Ch10As\Resources\BeadedBag.jpg");
    else if (this.lstBxBagType.Text == "Full Decorative-$50.00")
        pictureBox1.Image = new Bitmap(@"C:\Users\me\Desktop\Ch10As\Ch10As\Resources\FullDecoBag.jpg");
    else if (this.lstBxBagType.Text == "Pirate Design-$40.00")
        pictureBox1.Image = new Bitmap(@"C:\Users\me\Desktop\Ch10As\Ch10As\Resources\PirateBag.jpg");
    else if (this.lstBxBagType.Text == "Fringed-$25.00")
        pictureBox1.Image = new Bitmap(@"C:\Users\me\Desktop\Ch10As\Ch10As\Resources\FringedBag.jpg");
    else if (this.lstBxBagType.Text == "Leather-$80.00")
        pictureBox1.Image = new Bitmap(@"C:\Users\me\Desktop\Ch10As\Ch10As\Resources\LeatherBag.jpg");
    else if (this.lstBxBagType.Text == "Plain-$20.00")
        pictureBox1.Image = new Bitmap(@"C:\Users\me\Desktop\Ch10As\Ch10As\Resources\PlainBag.jpg");
}

ok so I just went back to look at the properties and changed it so that the image on the picture box changes to what selected when the picturebox is clicked but that's not how its supposed to be, how do i make it change automatically, im going to do some trial and error to see if i can find one to to that

Это было полезно?

Решение

If you're using a ListBox, check against the SelectedItem() property, not the Text() property:

        if (lstBxBagType.SelectedItem.ToString() == "Beaded-$45.00")
        {
            // do something
        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top