Results 1 to 4 of 4

Thread: [RESOLVED] [2005] remove items from listbox problem

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    35

    Resolved [RESOLVED] [2005] remove items from listbox problem

    hey guys,

    i have 2 listboxes, when i move items from one to another it works properly, but when i try to remove items from my second list box, sometimes it works and sometimes not! i guess it is my for loop:

    Dim i As Integer
    Dim lstcount As Integer
    lstcount = ListBox1.Items.Count
    For i = 0 To ListBox1.Items.Count - 1
    If ListBox1.Items(i).Selected = True Then
    ListBox1.Items.RemoveAt(i)
    End If
    Next

    does anyone know how to fix this problem??

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [2005] remove items from listbox problem

    Let's say that you select the items at index 0 and index 1. You start your loop at index 0 and remove the item. Once that item is removed, the item that was previously at index 1 is now at index 0 and the item that was at index 2 is now at index 1. Your loop moves on to index 1 and it finds that the item is not selected. You see the issue? You've skipped the item that was originally at index 1. Whenever you want to remove items from a collection, not just a ListBox, in a loop you should always start at the end and work towards the front. That way removing an item doesn't affect the indexes of those items that you are yet to visit. Instead of:
    VB Code:
    1. For i = 0 To ListBox1.Items.Count - 1
    you should use:
    VB Code:
    1. For i = ListBox1.Items.Count - 1 To 0 Step -1
    Note also that I have included the Step. You should ALWAYS include the Step when you are using the length of an array or collection as the limit for a loop, regardless of whether you are working forwards or backwards. That's because if the array or collection is empty and you haven't included the Step then an exception will be thrown. Take your code for example. If the ListBox was empty then your loop would be going from 0 to -1 and the Step would implicitly become -1. That means that your code would try to access the item at index 0, which does not exist. BAM! Unhandled exception. If you had specified the Step as 1 then the fact that the end point is less than the start point means that the loop would not be executed, as is proper.

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    35

    Re: [2005] remove items from listbox problem

    Man thanx a 10000000000000!!! I tried it and it worked perfectly!!! that's gr8!
    now i understand the concept of the remove item, thanx again!!!

  4. #4
    Hyperactive Member ahmad_iam's Avatar
    Join Date
    Jun 2005
    Location
    Pakistan
    Posts
    265

    Resolved Re: [2005] remove items from listbox problem

    Quote Originally Posted by jmcilhinney
    Let's say that you select the items at index 0 and index 1. You start your loop at index 0 and remove the item. Once that item is removed, the item that was previously at index 1 is now at index 0 and the item that was at index 2 is now at index 1. Your loop moves on to index 1 and it finds that the item is not selected. You see the issue? You've skipped the item that was originally at index 1. Whenever you want to remove items from a collection, not just a ListBox, in a loop you should always start at the end and work towards the front. That way removing an item doesn't affect the indexes of those items that you are yet to visit. Instead of:
    VB Code:
    1. For i = 0 To ListBox1.Items.Count - 1
    you should use:
    VB Code:
    1. For i = ListBox1.Items.Count - 1 To 0 Step -1
    Note also that I have included the Step. You should ALWAYS include the Step when you are using the length of an array or collection as the limit for a loop, regardless of whether you are working forwards or backwards. That's because if the array or collection is empty and you haven't included the Step then an exception will be thrown. Take your code for example. If the ListBox was empty then your loop would be going from 0 to -1 and the Step would implicitly become -1. That means that your code would try to access the item at index 0, which does not exist. BAM! Unhandled exception. If you had specified the Step as 1 then the fact that the end point is less than the start point means that the loop would not be executed, as is proper.
    I really appriciate your post dear jmcilhinney. You are nice man. Thanks for this complement.
    "Visual Studio .NET 2005/.NET Framework 2.0"

    The eyes are the greatest telescopes, one will ever need to study the universe. Let the heart be the tripod, the mind be the shaft, then let the eyes become the lens of that great telescope of which God gave to us all.

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