Results 1 to 6 of 6

Thread: Delete selecteditem in listview

  1. #1
    cops is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Sep 2008
    Posts
    4
    Reputation
    0

    Question Delete selecteditem in listview

    Helo expert..

    Please help me... I try to delete several item but always error..

    error code >>>> InvalidArgument=Value of '32' is not valid for 'index'. Parameter name: index

    Code:
     Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
            If MsgBox("Are You sure to delete all this data?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
              
                For i = 0 To ListView1.CheckedItems.Count - 1 
                    ListView1.Items.Remove(ListView1.CheckedItems(i))
                Next i
    
    
    
            End If
               
        End Sub

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,336
    Reputation
    1543
    First things first, in a ListView there is a difference between an item being checked and an item being selected. That's why there are SelectedItems and CheckedItems properties.

    As for the issue, the lower and upper bounds of your loop are calculated once only at the start. When you remove an item it is no longer checked, so the number of checked items is reduced. That means that your index is going to be invalid when you have removed half the items.

  3. #3
    cops is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Sep 2008
    Posts
    4
    Reputation
    0
    Thanks for the reply john.

    Yup.. i understand the difference between item being checked and being selected... sorry for wrong title.. it supposed to be checked item.

    can you tell me how i suppose to write the loop ?

    Thanks

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,336
    Reputation
    1543
    What do you think? Use a bit of logic. Obviously a For loop is not suitable so what are the other options? Think about how you would tackle a similar problem in the physical world and you'll find that the programming solution is basically the same. Programming doesn't exist in a vacuum.

  5. #5
    cops is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Sep 2008
    Posts
    4
    Reputation
    0
    Thanks Mate

    resolved using loop do until

  6. #6
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,336
    Reputation
    1543

Bookmarks

Posting Permissions

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