2011-12-20 3 views
0

私はWindowsフォームアプリケーションを作成するためにVB 2008 expressを使用しています。私はcb_faceという名前のコンボボックスを持っています。コンボボックスの項目は、 "for each"ループを使用して、自分のリソースフォルダから移入されたイメージファイル名です。アイテムが選択されると、画像をpicturebox1に表示したいと思います。いくつかの異なるコードを試しましたが、どれも画像を表示しません。私は何の誤りもありません。コメント行には、試行されたコードの一部が表示されます。Combobox.selecteditemからPicturebox.imageを表示する方法

あなたが持っている
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 

答えて

1

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.Image = Image.FromFile("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources\" & (CB_Face.SelectedItem.ToString) & ".bmp") 

これは、ファイルがBMPで、配置されていることをあなたのコードの残りの部分に基づいて想定していそのパスの残りのリソースディレクトリに保存します。

ディレクトリパス文字列に追加する場合は、&を使用して、同じ方法でファイル拡張子を読み取る必要があります。

+0

ありがとうございました。それは完璧に働いた。 – UBel

関連する問題