0

So I want a Listbox and when u press an element in the list box the picture box changes the picture so I have 4 picture boxes behind each other and I want to do

If listBox1.SelectedItem "Flowers" Then
pictureBox1.Show():
pictureBox2.Hide();
pictureBox3.Hide();
but it keep getting an error I can't use if with selected item help

I'm using C# WinForms (.NET Framework)

Vizel Leonid
  • 456
  • 7
  • 15
Bleak MC
  • 9
  • 1
  • Is that the actual code, because that looks closer to VB.Net than C#? C# would be something like `if(listBox1.SelectedItem == "Flowers") { ....}` – juharr Mar 28 '21 at 14:12
  • ill try it thanks also I'm using c# but I just tried that code and I believe it should work and yeah thats the actualy code – Bleak MC Mar 28 '21 at 14:15
  • 2
    'To believe'? Do you think that they will work if you believe enough or go into a trance? :) @juharr is right, and also above aside to execute, these block even cannot compile by csc compiler – gurkan Mar 28 '21 at 14:39

2 Answers2

0

Select picture box > Open properties window in Visual Studio > Click Image on properties window > Click ... button > Click project resource file > Click Import button and choose the image you want to upload. Next, double click on the list box. And write this code inside the (ListBoxName)_SelectedIndexChanged method.

if (listBox1.SelectedItem == "Flowers")
    pictureBox1.Image = Properties.Resources.Flowers;//Flowers is name of image
else if (listBox1.SelectedItem == "Others")
    pictureBox1.Image = Properties.Resources.OtherImage;//OtherImage is name of image

Now you will have 1 picture box and you will just change the image of the picture box.

turaltahmazli
  • 333
  • 2
  • 7
0

you can Use this in code behind

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (listBox1.SelectedItem)
            {
                case "Flowers":
                    pictureBox1.Show():
                    pictureBox2.Hide();
                    pictureBox3.Hide();
                    break;
                case "Other":
                    //....
                    break;
            }
        }
Reza Basereh
  • 103
  • 6