Click here to Skip to main content
15,896,486 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I am using the better listview control within my vb.net application but seem to have trouble using For Each loop statements as the syntax of the control is different from that of normal listview control within vb.net

here is the code I am trying to work with..

VB
Dim item As BetterListViewItem
                           For Each item In BetterListView1.Items
                               Dim Osubitem As String = item.SubItems(1).Text
                               If Osubitem = hiber Or Osubitem = FXSAPI Or Osubitem = setup1 Or Osubitem = setup2 Then
                                   item.Remove()
                               End If
                           Next



Here is the link where I have found the control..

http://www.componentowl.com/better-listview[^]


Currently the program loads with no error but does not work completely

thank you in advance
Posted
Comments
joshrduncan2012 19-Apr-13 15:58pm    
Please provide what output you are receiving and what you expect to receive.
Dale 2012 19-Apr-13 16:06pm    
I hope to exclude the items from the control if found is true

If Osubitem = hiber Or Osubitem = FXSAPI Or Osubitem = setup1 Or Osubitem = setup2 Then
item.Remove()

when the item is found the program freezes. any ideas?
José Amílcar Casimiro 19-Apr-13 16:14pm    
It's not possible. The underlying collection cannot be modified while it's being enumerated.
[no name] 19-Apr-13 15:59pm    
Why are you not asking their support people about their control? That is what their support people are for and they would know more about it than we would most likely.
Dale 2012 19-Apr-13 16:06pm    
I have emailed the support team there but hoped someone else on this site might know or have knowledge about this control.

I suggest to ask this question for the author or for support[^], but before see the documentation[^] and class reference[^].

Remeber, "Currently the program loads with no error but does not work completely" message is not informative at all. ;(
 
Share this answer
 
Comments
Dale 2012 19-Apr-13 16:56pm    
its not informative because I have no other information about the error. Where in the documentation can I view item handling with for each statements to exclude items if they are added to the subitems of the betterlistview? I have emailed and searched for the answer but cannot find anything.
The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.

http://msdn.microsoft.com/en-us/library/ttw7t8t6(v=vs.80).aspx[^]

A possible solution would be:
For i = BetterListView1.Items.Count - 1 To 0 Step -1
    Dim Osubitem As String = item.SubItems(1).Text
    Select Case Osubitem
	Case "hiber"
	 BetterListView1.Items.RemoveAt(i)
	Case "FXSAPI"
	 BetterListView1.Items.RemoveAt(i)
	Case "setup1"
	 BetterListView1.Items.RemoveAt(i)
	Case "setup2"
	 BetterListView1.Items.RemoveAt(i)
    End Select
Next



based on the information available in this page: http://www.componentowl.com/documentation/better-listview/data/chapter-collections.html[^]

I think you should check also: RemoveAll Method.
 
Share this answer
 
v2
Comments
Dale 2012 19-Apr-13 16:57pm    
Ok thank you.. Is there a better method for checking if a item has been added to a listview and if true remove it?
José Amílcar Casimiro 19-Apr-13 17:24pm    
I update the solution.
Dale 2012 19-Apr-13 19:13pm    
what is Item?

Dim Osubitem As String = item.SubItems(1).Text

it is not declared and betterlistview.subitems is not a member of that control
Dale 2012 19-Apr-13 19:22pm    
this solution that I have come up with works to show a message box once the string is found in the better listview but for some reason will not work if I replace msgbox("found") with item.remove?

Dim Item As BetterListViewItem
For Each Item In BetterListView1
Dim subitem As BetterListViewSubItem
subitem = Item.SubItems(1)
If subitem.Text = hiber Or subitem.Text = FXSAPI Then
MsgBox("found")
End If
Next
Dale 2012 19-Apr-13 19:11pm    
thank you but I have read through this and cannot see where you came up with this solution. The remove all area is indeed in the MSDN link you provided but does not achieve the desired results.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900