[RESOLVED] Opening images with ListBox
Hello, I have a ListBox.
When I load up an image, a entry in added to the ListBox with the location of the image file. I also have a PictureBox... when you click a item in the listbox, I need the PictureBox to load the image associated with the ListBox entry.
I tried this, but of I course it didn't work:
Private Sub import_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles import.Click
Dim dlg As DialogResult = openfile.ShowDialog()
Dim filename As String = openfile.FileName
Images.Items.Add(filename)
Dim selectpic = New Bitmap(Images.SelectedItem.Text)
PictureBox1.Image = selectpic
End Sub
Now I know this is incorrect:
Dim selectpic = New Bitmap(Images.SelectedItem.Text)
But I am stumped on what to do, can I set a variable for each new entry which contains the image?
Re: Opening images with ListBox
So the ListBox contains the path to the image right?
vb.net Code:
PictureBox1.Image = Image.FromFile(Images.SelectedItem.ToString)
Re: Opening images with ListBox
Quote:
Originally Posted by
formlesstree4
So the ListBox contains the
path to the image right?
vb.net Code:
PictureBox1.Image = Image.FromFile(Images.SelectedItem.ToString)
Assuming that the ListBox contains Strings in the first place:
vb.net Code:
PictureBox1.Image = Image.FromFile(Images.Text)
Re: Opening images with ListBox
Thanks, works great. Only thing is, you always have to have a image selected or you'll get a ArgumentException error.
Re: Opening images with ListBox
Quote:
Originally Posted by
iMiles
Thanks, works great. Only thing is, you always have to have a image selected or you'll get a ArgumentException error.
That would be what validation is for. Test that an item is selected before using the selected item.