如何从 Combobox.selecteditem 显示 Picturebox.image
我正在使用 VB 2008 Express 创建 Windows 窗体应用程序。我有一个名为 cb_face 的组合框。组合框中的项目是使用“foreach”循环从我的资源文件夹填充的图像文件名。当选择一个项目时,我想在 picturebox1 中显示图像。我尝试了几种不同的代码,但没有一个显示图像。我没有收到任何错误。注释行显示了一些已经尝试过的代码。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的位置:
将其更改为类似以下内容:
这假定根据代码的其余部分,该文件是 bmp 并且位于该路径其余部分的 Resources 目录中。
您需要使用 &附加到目录路径字符串,然后以相同的方式读取文件扩展名。
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.