Help deleting files in a ListBox
Alright, the title is a bit misleading.
So I have a program that allows the user to load up some images, which appear in a ListBox. You can click the imported images and they load into a PictureBox.
Here's the code:
vb.net Code:
Public Class Form1
Private Sub import_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles import.Click
Dim result As DialogResult = openfile.ShowDialog
Images.Items.Add(openfile.FileName)
Images.SelectedIndex = 0
PictureBox1.Image = Image.FromFile(Images.Text)
End Sub
Private Sub Images_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Images.SelectedIndexChanged
PictureBox1.Image = Image.FromFile(Images.Text)
End Sub
Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
Images.Items.Remove(Images.SelectedItem)
End Sub
End Class
Now here is my problem: when the user tries to delete a image, an exception occurs like this:
Code:
System.ArgumentException was unhandled
The path is not of a legal form.
You can load up the code and see for yourself. Now I need to be able to delete these images from the ListBox. The exception occurs because the image is now null, and can't load.
Any ideas?
Re: Help deleting files in a ListBox
try
With Images
.Items.Remove(Images.SelectedItem)
End With
Re: Help deleting files in a ListBox
Can you supply the illegal path: it should be contained within the exception details.
Re: Help deleting files in a ListBox
Actually as soon as you remove any item, the SelectedIndexChanged will fire. But as there is no current selection (after deletion), it will error out.
Handle it like this:
vb.net Code:
Private Sub Images_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Images.SelectedIndexChanged
If Images.SelectedItem IsNot Nothing Then
PictureBox1.Image = Image.FromFile(Images.Text)
Else
PictureBox1.Image = Nothing
End If
End Sub
Re: Help deleting files in a ListBox
Works great now! However if there's more than one image and you delete one, I don't want the picture to be nothing. Instead, it goes to the other last image on the list.
Re: Help deleting files in a ListBox
So you code it accordingly. Your Delete button should handle the situation. It should ensure that before removing the item, it has first switched over to the next item in list.
vb.net Code:
Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
'save current selected item into a variable
Dim li As Object = Images.SelectedItem
' move selection to next item
If Images.SelectedIndex < Images.Items.Count - 1 Then
Images.SelectedIndex += 1
ElseIf Images.SelectedIndex > 0 Then
Images.SelectedIndex -= 1
End If
' delete your item from list
Images.Items.Remove(li)
End Sub
Re: Help deleting files in a ListBox
Here is another way:
(more close like human thinking - first remove item then move selection to next item)
vb.net Code:
Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
'save current selected index into a variable
Dim index As Integer = Images.SelectedIndex
' delete the item from list
Images.Items.Remove(Images.SelectedItem)
' move selection to next item
If index < Images.Items.Count - 1 Then
Images.SelectedIndex += 1
ElseIf index > 0 Then
Images.SelectedIndex -= 1
End If
End Sub