Results 1 to 8 of 8

Thread: [RESOLVED][2008]Listview subitem search Q

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Resolved [RESOLVED][2008]Listview subitem search Q

    this is what I was able to find so far, but its still not working. im shure im on the right track tho. Im trying to search subitems for text & if found remove the whole row, exept the last bold one ->item2 item12

    So I have 2 columns: the problem is that the code works after item 12, until item13 is reached, then i get error InvalidArgument=Value of '1' is not valid for 'index'. Parameter name: index

    Code:
    Column1     Column2
    item1         item11
    item2         item12
    item3
    thats my code.
    Code:
            For Each ListViewItem In ListView1.Items
                If ListViewItem.SubItems(1) Is Nothing Then
                    MessageBox.Show("nothing")
                    exit for
                Else
                    ListView1.Items.RemoveAt(0)
                End If
    EDIT: see post 6 & 7 for solution
    Last edited by goldenix; Apr 19th, 2008 at 10:58 AM.

    M.V.B. 2008 Express Edition

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008]Listview subitem search Q

    Don't use a For Each loop if you want to remove items from the collection. Use a For loop and count backwards. That way removing an item doesn't affect the positions of the items you're still yet to visit.
    vb.net Code:
    1. For index As Integer = myListView.Items.Count - 1 To 0 Step -1
    2.     If myListView.Items(index).SubItems(1).Text = someText Then
    3.         myListView.Items.RemoveAt(index)
    4.     End If
    5. Next index
    Not to mention that you're not doing it right even if you were to use a For Each loop. You're removing the item at index zero every time, even if it's not the item at index zero that you matched. In that case you'd Remove(item), not RemoveAt(index). If you use a For Each loop then you have an item in the collection, not an index. In a For loop you have an index.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: [2008]Listview subitem search Q

    Quote Originally Posted by jmcilhinney
    ..
    Well I tried something similar before( waisted 6+ hours or so experimenting..But got same error as I mentioned before. Same error is also present if i use your code.

    I mean i dont get it. if text is present it should remove the row, & if not do nothing, but instead it errors.. really weird.

    M.V.B. 2008 Express Edition

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: [2008]Listview subitem search Q

    how about this ...
    Code:
            Dim strItem As Integer = 12
            For x As Integer = ListView1.Items.Count - 1 To 0 Step -1
                '/// if there is no subitem listed, the Subitems count still shows as 1
                '/// if you have a Subitem, the Subitem count is 2 ( or more if you have several subitems in a row. )
                If ListView1.Items(x).SubItems.Count > 1 Then
                    If ListView1.Items(x).SubItems(1).Text = "item" & strItem.ToString Then
                        ListView1.Items.Remove(ListView1.Items(x))
                        strItem -= 1
                    End If
                End If
            Next
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: [2008]Listview subitem search Q

    Quote Originally Posted by dynamic_sysop
    how about this ...
    aa i see ur tying to fool the program, but nothing will happen, because this value wont be found. VB. will t hink, its 1 value & will try to search for it. well i did experiment but it didnt work.

    is there any other way to do this? I found some ppl mentioning FindItemWithText but i havent been able to find a working sample to understand how to use it.

    M.V.B. 2008 Express Edition

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008]Listview subitem search Q

    I think I may have misunderstood. Are you saying that your items may have different numbers of subitems? If that's the case then you have to test the number of subitems first, then only compare the subitem text if the subitem exists. You can't get the subitem at index 1 if there is no subitem at index 1.
    vb.net Code:
    1. Dim items As ListView.ListViewItemCollection = Me.ListView1.Items
    2. Dim item As ListViewItem
    3.  
    4. For index As Integer = items.Count - 1 To 0 Step -1
    5.     item = items(index)
    6.  
    7.     If item.SubItems.Count > 1 AndAlso item.SubItems(1).Text = someText Then
    8.         items.Remove(item)
    9.     End If
    10. Next index
    That said, it would be far better practice to add the same number of subitems to each item. If there's no text to display in a particular column the create a subitem and set its Text to String.Empty. With this issue would never have occurred.

    EDIT: Hmmm... I just realised that dynamic_sysop posted basically the same thing. What we've posted so far should certainly work, so I can only conclude that we have not understaood your issue properly. Please try to explain clearly and fully. Maybe you should start with exactly how you populated the ListView in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: [2008]Listview subitem search Q

    i just had a look at FindItemWithText in a form & it's very self explanetory, you notice it says 2 overrides, well they are include subitems & start index
    so i put this together as an example...
    Code:
            '/// YOUR ITEMS TO SEARCH FOR WOULD GO HERE ...
            Dim strArray As String() = {"item11", "item12", "and so on..."}
            For Each s As String In strArray
                For x As Integer = ListView1.Items.Count - 1 To 0 Step -1
                    '///SET THE 2ND PARAMETER TO TRUE TO INCLUDE SUBITEMS
                    Dim l As ListViewItem = ListView1.FindItemWithText(s, True, x)
                    If Not l Is Nothing Then
                        ListView1.Items.Remove(l)
                    End If
                Next
            Next
    hope it helps.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: [2008]Listview subitem search Q

    Quote Originally Posted by jmcilhinney
    ..
    Quote Originally Posted by dynamic_sysop
    ..
    @jmcilhinney
    you understood me correctly the second column is only filled half way, after that there are no items. & your last example does work. I get it now. I didnt know you can count subitems & AndAlso was also new to me.

    @dynamic_sysop
    Your FindItemWithText sample does not error also.

    Thank you both.

    M.V.B. 2008 Express Edition

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