Results 1 to 4 of 4

Thread: Need help > How to remove data from listview1 and 2 in the same time

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    2

    Exclamation Need help > How to remove data from listview1 and 2 in the same time

    Name:  lv.jpg
Views: 522
Size:  35.1 KB

    When double click at listview (on top) then the below listview should remove data also

    I've been trying to do but if I remove item no.2 which it had 2 rows on below listview. Then it's shown...

    "InvalidArgument=Value of '3' is not valid for 'index'. Parameter name: index"

    Code

    For i = 0 To lvPO.SelectedItems.Count - 1
    Dim lvi As ListViewItem
    lvi = lvPO.SelectedItems(i)
    'MessageBox.Show(lvPO.Items(lvPO.SelectedItems(i).Index).SubItems(1).Text)

    For k = 0 To lvSeparate.Items.Count - 1

    If lvSeparate.Items(k).SubItems(1).Text = lvPO.Items(lvPO.SelectedItems(i).Index).SubItems(1).Text Then

    lvSeparate.Items.Remove(lvSeparate.Items(k))

    End If

    Next

    lvPO.Items.Remove(lvi)
    Next



    *** Thank you in advance

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Need help > How to remove data from listview1 and 2 in the same time

    try this:

    Code:
    For i = 0 To lvPO.SelectedItems.Count - 1
        Dim lvi As ListViewItem
        lvi = lvPO.SelectedItems(i)
        'MessageBox.Show(lvPO.Items(lvPO.SelectedItems(i).Index).SubItems(1).Text)
    
        For k = lvSeparate.Items.Count - 1 to 0 step -1
    
            If lvSeparate.Items(k).SubItems(1).Text = lvPO.Items(lvPO.SelectedItems(i).Index).SubItems(1).Text Then
    
                lvSeparate.Items.Remove(lvSeparate.Items(k))
    
            End If
    
        Next
    
        lvPO.Items.Remove(lvi)
    Next

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    2

    Re: Need help > How to remove data from listview1 and 2 in the same time

    Clear!!

    Thank you very much.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Need help > How to remove data from listview1 and 2 in the same time

    what I changed...

    Your original code was removing items from a collection whilst looping through that collection. This causes the collection to change and subsequently it refers to items which are no longer there.
    The way to remove items from a collection in a loop is to cycle backwards through the collection.

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