I’m using VB 2008 express to create a windows form application. I have a combobox named cb_face. The items in the combobox are image file names populated from my resource folder using a “for each” loop. When an item is selected I would like to display the image in picturebox1. I have tried several different codes but none of them display the image. I am not getting any errors. The comment lines show some of the code that has been tried.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ImgFolder As New IO.DirectoryInfo("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources")
Dim ImgFile As IO.FileInfo() = ImgFolder.GetFiles("*.bmp")
Dim info As IO.FileInfo
For Each info In ImgFile
Dim FaceName As String = IO.Path.GetFileNameWithoutExtension(info.FullName)
CB_Face.Items.Add(FaceName)
Next
End Sub
Private Sub CB_Face_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CB_Face.SelectedIndexChanged ', CB_Type.SelectedIndexChanged
If CB_Face.SelectedValue IsNot Nothing Then
'Load the image from the full file path
'PictureBox1.ImageLocation = CStr(CB_Face.SelectedItem.ToString)
'PictureBox1.Image = CB_Face.Items(CB_Face.SelectedItem).ItemData
'Dim pic = CType(My.Resources.ResourceManager.GetObject(CStr(CB_Face.SelectedItem)), Image)
'PictureBox1.Image = pic
'PictureBox1.Image = CB_Face.SelectedItem
PictureBox1.Image = Image.FromFile("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources"(CB_Face.SelectedItem.ToString).ToString)
'PictureBox1.ImageLocation = CB_Face.SelectedItem(Name)
'Try
'PictureBox1.Image = Image.FromFile(CB_Face.SelectedItem.ToString)
'Catch ex As Exception
'End Try
'PictureBox1.Image = DirectCast(CB_Face.SelectedItem, Image)
'CType(CB_Face.SelectedItem, Image)
End If
End Sub
Where you have:
Change that to something more like:
This assumes based on the rest of your code that the file is a bmp and is located in the Resources directory of the rest of that path.
You need to use & to append to your directory path string and then readd the file extension in the same way.