0

In Visual Studio, in one of the window forms, I've got a ListBox that is retrieving it'information from a data table in SQL Server. For this data table the primary key is called ID. In the same window form I've added a picturebox, where I would like the images to be generated depending on the selection in the listbox. The images are saved in a folder on the project folder.

I'm already using a method to populate the listbox so maybe I can use that to determine which image needs to be displayed:

 static SqlCommand com = new SqlCommand("SELECT *,CONCAT(name,' ',year) as date_bikes FROM model", conn);

 SqlDataAdapter adaptb = new SqlDataAdapter(com);

 DataTable bikeT = new DataTable();

void Method1() --this method fills the checkedlistbox

 {

 adaptb.Fill(bikeT);

 bikes.Items.Clear();

 bikes.DataSource = bikeT;

 bikes.ValueMember = "ID";

 bikes.DisplayMember = "namemod";

 }

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

    {
        foreach (object itemChecked in listBox1.Items)
        {
            DataRowView castedItem = itemChecked as DataRowView;
            string name = castedItem["namemod"].ToString();
        }
      if (name == "Bike 1")
        {
          pictureBox1.Image = Image.FromFlie("file path")
        }
      else if (name == "Bike 2")
        {
          pictureBox1.Image = Image.FromFlie("file path")
        }
    and so on...
Daniel
  • 5
  • 5
  • With use of simple IF and checked change event determine which item is checked, then set picturebox.Image property to path of your picture? – Veljko89 Jan 23 '18 at 08:30
  • Can you elaborate a bit on that? What do you mean by 'checked change event determine which item is checked' ? – Daniel Jan 23 '18 at 08:47
  • First you would have to set `CheckedListBox1.SelectionMode = SelectionMode.One;` , meaning only single item from your checked list box could be checked, after that check answer from this question https://stackoverflow.com/questions/4875540/how-to-get-value-of-checked-item-from-checkedlistbox , use of `for loop` will get you that single item that is checked and with use of `if` you set appropriate picture to your picture box, and all of that would be inside of event `ItemCheck` of your `checkedlistbox` control – Veljko89 Jan 23 '18 at 08:53
  • @Veljko89 I've decided to go with a ListBox instead of a CheckedListBox. Are you able to provide me with an example of how it would be best to implement this so I can generate the image. – Daniel Jan 24 '18 at 11:23
  • It's same principle, literate through ListBox.Items, have `if (item == checked)` select correct picture's url – Veljko89 Jan 24 '18 at 11:32
  • what exactly (what kind of information) is in the `checkedlist` ? how is this related to the images that you want to load? – Mong Zhu Jan 24 '18 at 12:04
  • @Mong Zhu so I've actually changed to a ListBox so only one item can be selected. And in the listbox I've got bicycle models that are driven from a table in Sql Server. So depending on the bike selected I want to generate a specific image in a PictureBox below the ListBox. And the main columns in the table are ID (numeric) which is the PK and model_name (nvarchar). I got some other columns but don't think they are relevant for this. – Daniel Jan 24 '18 at 12:10
  • how do you intend to find the image that relates to a certain bike? – Mong Zhu Jan 24 '18 at 12:47
  • I have them saved in a folder in the project. And then based on the ListBox item I want to generate the appropriate image from the folder. – Daniel Jan 24 '18 at 12:49
  • ok then where exactly is your problem? you don't know how to load a *.jpg file into a picturebox? – Mong Zhu Jan 24 '18 at 12:52
  • So I've added above how far I went, with iterating through the list. Now how can I map that to the images? Maybe it sounds stupid :) – Daniel Jan 24 '18 at 13:40
  • @Mong Zhu so I've actually completed the code with the if statement as well, but now the image does not change in line with the ListBox selection. The first image shows every time. Any suggestions? – Daniel Jan 25 '18 at 07:33
  • " how can I map that to the images? " this is what I was asking you the whole time :) see: "how do you intend to find the image that relates to a certain bike?" ok you could create a new column in your datatable `path` and supply the paths for each bike when you load the data. But if you want to stick to the if-clauses you need to write each path according to the propper bike. Then you have a clear mapping – Mong Zhu Jan 25 '18 at 08:08
  • @Mong Zhu as per my code above, I've added the path for each bike but still only the same image is showing. – Daniel Jan 25 '18 at 09:02
  • actually 1) you cannot use the variable `name` outside of the for loop. your code will not compile 2) if you run through your loop and check the `name` afterwards you will have always the name of the last bike in the list. this is why you load always the same picture. -> you need to use the `listBox1.SelectedItem` and not run with a loop, because you want only the picture of the selected bike am I right? – Mong Zhu Jan 25 '18 at 10:12
  • @Mong Zhu exactly, only the selected bike. So instead of name I should use listbox1.SelectedItem. So in that case I wouldn't need the for loop just the if one, right? – Daniel Jan 25 '18 at 11:00
  • Yes, exactly, I posted it as answer – Mong Zhu Jan 25 '18 at 11:32

1 Answers1

0

You need to take the SelectedItem instead of looping through the array:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataRowView castedItem = listBox1.SelectedItem as DataRowView;
    string name = castedItem["namemod"].ToString();

    if (name == "Bike 1")
    {
      pictureBox1.Image = Image.FromFlie("file path")
    }
    else if (name == "Bike 2")
    {
      pictureBox1.Image = Image.FromFlie("file path")
    }
and so on...
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76