Results 1 to 7 of 7

Thread: Help deleting files in a ListBox

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    34

    Unhappy 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:
    1. Public Class Form1
    2.  
    3.     Private Sub import_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles import.Click
    4.         Dim result As DialogResult = openfile.ShowDialog
    5.         Images.Items.Add(openfile.FileName)
    6.         Images.SelectedIndex = 0
    7.         PictureBox1.Image = Image.FromFile(Images.Text)
    8.     End Sub
    9.  
    10.     Private Sub Images_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Images.SelectedIndexChanged
    11.         PictureBox1.Image = Image.FromFile(Images.Text)
    12.     End Sub
    13.  
    14.     Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
    15.         Images.Items.Remove(Images.SelectedItem)
    16.     End Sub
    17. 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?

  2. #2
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Help deleting files in a ListBox

    try

    With Images
    .Items.Remove(Images.SelectedItem)
    End With

  3. #3
    Lively Member eatmycode's Avatar
    Join Date
    Mar 2010
    Location
    Kingston upon Hull
    Posts
    74

    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

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Sub Images_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Images.SelectedIndexChanged
    2.     If Images.SelectedItem IsNot Nothing Then
    3.         PictureBox1.Image = Image.FromFile(Images.Text)
    4.     Else
    5.         PictureBox1.Image = Nothing
    6.     End If
    7. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    34

    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.

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
    2.     'save current selected item into a variable
    3.     Dim li As Object = Images.SelectedItem
    4.  
    5.     ' move selection to next item
    6.     If Images.SelectedIndex < Images.Items.Count - 1 Then
    7.         Images.SelectedIndex += 1
    8.     ElseIf Images.SelectedIndex > 0 Then
    9.         Images.SelectedIndex -= 1
    10.     End If
    11.  
    12.     ' delete your item from list
    13.     Images.Items.Remove(li)
    14. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete.Click
    2.     'save current selected index into a variable
    3.     Dim index As Integer = Images.SelectedIndex
    4.  
    5.     ' delete the item from list
    6.     Images.Items.Remove(Images.SelectedItem)
    7.  
    8.     ' move selection to next item
    9.     If index < Images.Items.Count - 1 Then
    10.         Images.SelectedIndex += 1
    11.     ElseIf index > 0 Then
    12.         Images.SelectedIndex -= 1
    13.     End If
    14. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width