I have around 20 pictures in picturebox and i want to loop through all images and add it's name to a combobox then i use the combobox to change the picture in the picturebox. I Dont know how to loop through picturebox images.
2 Answers
To populate a ComboBox with images from resources :
For Each dicEntry As DictionaryEntry In resourceSet.OfType(Of Object)()
If TypeOf (dicEntry.Value) Is Drawing.Image Then
ComboBox1.Items.Add(dicEntry.Key.ToString())
End If
Next
To select the image and set it to a PictureBox :
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim comboBox As ComboBox = CType(sender, ComboBox)
Dim sSelectedItem = CType(comboBox.SelectedItem, String)
Dim img As Image = CType(My.Resources.ResourceManager.GetObject(sSelectedItem), Image)
PictureBox1.BackgroundImage = img
End Sub

- 1,465
- 1
- 9
- 11
-
Is there a way to specify a folder inside resources folder? – Shan Jun 15 '19 at 17:12
There are a few things to address here. Most importantly, Image
objects don't have a name, so there's nothing to list. Maybe you mean the names of the files that the Image
objects were created from, but that's something different. You won't be able to get those from the PictureBox
controls unless you set the ImageLocation
properties to load the files. Assuming that you have done that, you could get the name of each file from the PictureBoxes
like this:
Dim fileNames = Controls.OfType(Of PictureBox)().
Select(Function(pb) IO.Path.GetFileName(pb.ImageLocation))
That still doesn't really make sense though. It seems like getting the names of the files before loading the Images
would make more sense. You haven't really explained enough to provide a confident solution though.

- 50,448
- 5
- 26
- 46
-
Your update is of no value. There's no indication of what the relationship is between the list of names you have displayed and the `Images` you describe. You've addressed nothing that I raised in my answer. – jmcilhinney Jun 15 '19 at 16:18