I am having a combo box in my form and i want to open a image using that now whenever i select any option from the combo box different different images should be displayed what cud be the necessary code and what event should be used to write the necessary code for it plz guide with all necessary deatils along with a example code..

Recommended Answers

All 4 Replies

1/to use image in the form you need PictureBox Control
2/in order to change the picture whenever the user select item from the comboBox you use the SelectedIndexChanged event, you can get the item that user selected by

dim selectedItem as string
selectedItem = ComboBox.Text

dim selectedItem as string
selectedItem = ComboBox.Text

why didn't use selectedItem = ComboBox.SelectedItem ?
and there are path on combobox item to loaded?

i am opening the image frm the combo box the image is placed in a picture box now if my first item in the list be x then what code should be written to open the image in the picture box

Do you store the file path in your combo box? Like this:

ComboBox1.Items.Clear()
ComboBox1.Items.Add("D:\Download\Image1.jpg")
ComboBox1.Items.Add("D:\Download\Image2.jpg")

then you get the image with:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  '
  ' Load picture from the file

  Try
    PictureBox1.Image = Image.FromFile(ComboBox1.SelectedItem.ToString)
  Catch ex As Exception
    ' Handle exception
  End Try

End Sub

ComboBox1.SelectedItem is of the type object so you have to cast it to the string.
However, if you store "friendly names" in your combo box, you need some mapping to file names. Here's a one way to do that:

Private ImagePaths() As String

ComboBox1.Items.Clear()
ComboBox1.Items.Add("My dog")
ComboBox1.Items.Add("My wife")
ReDim ImagePaths(1)
ImagePaths(0) = "D:\Download\Image1.jpg"
ImagePaths(1) = "D:\Download\Image2.jpg"

and then you do the mapping to get the image:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  '
  ' Load picture from the file

  Try
    PictureBox1.Image = Image.FromFile(ImagePaths(ComboBox1.SelectedIndex))
  Catch ex As Exception
    ' Handle exception
  End Try

End Sub
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.