[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
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:
For index As Integer = myListView.Items.Count - 1 To 0 Step -1
If myListView.Items(index).SubItems(1).Text = someText Then
myListView.Items.RemoveAt(index)
End If
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.
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. :duck:
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
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.
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:
Dim items As ListView.ListViewItemCollection = Me.ListView1.Items
Dim item As ListViewItem
For index As Integer = items.Count - 1 To 0 Step -1
item = items(index)
If item.SubItems.Count > 1 AndAlso item.SubItems(1).Text = someText Then
items.Remove(item)
End If
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.
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.
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.