|
-
Mar 11th, 2010, 04:24 PM
#1
Thread Starter
Member
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?
-
Mar 11th, 2010, 04:45 PM
#2
Frenzied Member
Re: Help deleting files in a ListBox
try
With Images
.Items.Remove(Images.SelectedItem)
End With
-
Mar 11th, 2010, 04:46 PM
#3
Lively Member
Re: Help deleting files in a ListBox
Can you supply the illegal path: it should be contained within the exception details.
Technik ... Kniff, die Welt so einzurichten, dass wir sie nicht erleben mussen
Max Frisch
-
Mar 11th, 2010, 05:20 PM
#4
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
-
Mar 11th, 2010, 10:16 PM
#5
Thread Starter
Member
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.
Last edited by iMiles; Mar 11th, 2010 at 10:24 PM.
-
Mar 12th, 2010, 02:27 AM
#6
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
-
Mar 12th, 2010, 03:06 AM
#7
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|