Enigma04 Enigma04 - 10 days ago 6
Vb.net Question

Search folder based on listbox to populate picturebox

I am trying to populate a picturebox with a listbox from a file.

I have the following code (which works fine). However, I would like to save some coding and search a folder for a file that matches the filename selected from the listbox. I have tried a few different things but am not nearly fluent enough with VB yet to make this work. Any help would be greatly appreciated.

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged

If ListBox1.SelectedItem = "test1" Then
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.Image = Image.FromFile("c:Desktop\images\test1.jpg")


Else
If ListBox1.SelectedItem = "test2" Then
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.Image = Image.FromFile("c:Desktop\images\test2.jpg")
Else
MessageBox.Show("No Such File")
End If
Open_Button.Visible = True
Open_Label.Visible = True
Open_List.Visible = True
End Sub

Answer

I have it! Thank You Marc for your help.

 Dim file = System.IO.Path.Combine("C:\desktop\etc\", ListBox1.SelectedItem) + ".jpg"
    If ListBox1.SelectedItem IsNot Nothing Then

        If System.IO.File.Exists(file) Then

            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            PictureBox1.Image = Image.FromFile(file)

        Else


            MessageBox.Show("No Image")
        End If
    End If
        Open_Button.Visible = True
        Open_Label.Visible = True
        Open_List.Visible = True
End Sub