473,473 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Weird problem with listview and collection

I have a listview control and a collection object right now that I'm trying
to pass information to and from. Whenever I click on the checkbox, I want
it to remove certain listview items and add them to the collection.
Whenever I uncheck the checkbox, I want to add the items back into the
listview from the collection, and remove them from the collection so I can
do the process multiple times. The listview tag is being used as a key, so
I know which item to remove. It seems that it adds 29 items to the
collection, but then when I go to add them back to the listview, it only
adds 10. What am I doing wrong here?

Dim tmpItem As ListViewItem

If chkOnlyExternal.Checked = True Then

'Remove internal users.

For Each lvitem As ListViewItem In lstUsers.Items

If db.IsInternalUser(lvitem.Tag.ToString) Then

tmpItem = lvitem

lvCollection.Add(lvitem, lvItem.Tag.ToString)

lvitem.Remove()

rmCount += 1

End If

Next

MessageBox.Show("Removed from listview: " & rmCount.ToString)

Else

MessageBox.Show("Collection count: " & lvCollection.Count.ToString)

If lvCollection.Count > 0 Then

For Each lvItem As ListViewItem In lvCollection

lstUsers.Items.Add(lvItem)

lvCollection.Remove(lvItem.Tag.ToString)

addCount += 1

Next

MessageBox.Show("Added back to listview: " & addCount.ToString)

End If

End If


Nov 21 '05 #1
3 2135
This is a common mistake/mis-conception.
You should not modify a collection while iterating through it.
In fact, some collection implementations will raise an error if you attempt
to alter (add or remove) the contents while enumerating over it.

Here is what is happening...
Say you have a collection with 5 items.
You are iterating through it, and you are on item 2, so your enumeration
pointer is at 2.
You remove the current item.
There are now 4 items in the collection.
Your enumeration pointer is still at 2, but old item 2 is gone, your current
item should be old item 3.
You move to the next item 3, but that really steps to old item 4.
Same thing would happen if you dynamically resized an array while stepping
through it.

Make sense?
So if you step through a collection, removing the current item, you end up
only getting every other item.

There are numerous ways to get around this.
If you are using keys, then you could step through the collection adding
each item to the other. But keep track of which items were moved. Then after
you have finished with the collection, go back and remove the ones you moved
by key. If you are moving everything, then you could just empty the
collection.

Treat the collection like an array and use indexes (For index) instead of
For Each, and step backwards through the collection. Instead of going from
beginning to end, which causes thing to move up when removed, go from end to
beginning.

There are other ways to deal with this as well, but stepping backward I
believe might be the most common.

Gerald

"Matt Michael" <ih***@spamforme.com> wrote in message
news:eM**************@TK2MSFTNGP15.phx.gbl...
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want
it to remove certain listview items and add them to the collection.
Whenever I uncheck the checkbox, I want to add the items back into the
listview from the collection, and remove them from the collection so I can
do the process multiple times. The listview tag is being used as a key, so I know which item to remove. It seems that it adds 29 items to the
collection, but then when I go to add them back to the listview, it only
adds 10. What am I doing wrong here?

Dim tmpItem As ListViewItem

If chkOnlyExternal.Checked = True Then

'Remove internal users.

For Each lvitem As ListViewItem In lstUsers.Items

If db.IsInternalUser(lvitem.Tag.ToString) Then

tmpItem = lvitem

lvCollection.Add(lvitem, lvItem.Tag.ToString)

lvitem.Remove()

rmCount += 1

End If

Next

MessageBox.Show("Removed from listview: " & rmCount.ToString)

Else

MessageBox.Show("Collection count: " & lvCollection.Count.ToString)

If lvCollection.Count > 0 Then

For Each lvItem As ListViewItem In lvCollection

lstUsers.Items.Add(lvItem)

lvCollection.Remove(lvItem.Tag.ToString)

addCount += 1

Next

MessageBox.Show("Added back to listview: " & addCount.ToString)

End If

End If

Nov 21 '05 #2
Gerald,

Thanks for responding, I see now where I made my error. I'm clearing the
collection every time now, and everything is working just fine for me.
Thanks a lot, this would have bothered me all afternoon!

-Matt
"Gerald Hernandez" <Cablewizard@sp*********@Yahoo.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
This is a common mistake/mis-conception.
You should not modify a collection while iterating through it.
In fact, some collection implementations will raise an error if you
attempt
to alter (add or remove) the contents while enumerating over it.

Here is what is happening...
Say you have a collection with 5 items.
You are iterating through it, and you are on item 2, so your enumeration
pointer is at 2.
You remove the current item.
There are now 4 items in the collection.
Your enumeration pointer is still at 2, but old item 2 is gone, your
current
item should be old item 3.
You move to the next item 3, but that really steps to old item 4.
Same thing would happen if you dynamically resized an array while stepping
through it.

Make sense?
So if you step through a collection, removing the current item, you end up
only getting every other item.

There are numerous ways to get around this.
If you are using keys, then you could step through the collection adding
each item to the other. But keep track of which items were moved. Then
after
you have finished with the collection, go back and remove the ones you
moved
by key. If you are moving everything, then you could just empty the
collection.

Treat the collection like an array and use indexes (For index) instead of
For Each, and step backwards through the collection. Instead of going from
beginning to end, which causes thing to move up when removed, go from end
to
beginning.

There are other ways to deal with this as well, but stepping backward I
believe might be the most common.

Gerald

"Matt Michael" <ih***@spamforme.com> wrote in message
news:eM**************@TK2MSFTNGP15.phx.gbl...
I have a listview control and a collection object right now that I'm

trying
to pass information to and from. Whenever I click on the checkbox, I
want
it to remove certain listview items and add them to the collection.
Whenever I uncheck the checkbox, I want to add the items back into the
listview from the collection, and remove them from the collection so I
can
do the process multiple times. The listview tag is being used as a key,

so
I know which item to remove. It seems that it adds 29 items to the
collection, but then when I go to add them back to the listview, it only
adds 10. What am I doing wrong here?

Dim tmpItem As ListViewItem

If chkOnlyExternal.Checked = True Then

'Remove internal users.

For Each lvitem As ListViewItem In lstUsers.Items

If db.IsInternalUser(lvitem.Tag.ToString) Then

tmpItem = lvitem

lvCollection.Add(lvitem, lvItem.Tag.ToString)

lvitem.Remove()

rmCount += 1

End If

Next

MessageBox.Show("Removed from listview: " & rmCount.ToString)

Else

MessageBox.Show("Collection count: " & lvCollection.Count.ToString)

If lvCollection.Count > 0 Then

For Each lvItem As ListViewItem In lvCollection

lstUsers.Items.Add(lvItem)

lvCollection.Remove(lvItem.Tag.ToString)

addCount += 1

Next

MessageBox.Show("Added back to listview: " & addCount.ToString)

End If

End If


Nov 21 '05 #3
psh... you kids these days at Reschini and your crazy ideas :P
"Matt Michael" <ih***@spamforme.com> wrote in message
news:eM**************@TK2MSFTNGP15.phx.gbl...
I have a listview control and a collection object right now that I'm trying
to pass information to and from. Whenever I click on the checkbox, I want
it to remove certain listview items and add them to the collection.
Whenever I uncheck the checkbox, I want to add the items back into the
listview from the collection, and remove them from the collection so I can
do the process multiple times. The listview tag is being used as a key, so
I know which item to remove. It seems that it adds 29 items to the
collection, but then when I go to add them back to the listview, it only
adds 10. What am I doing wrong here?

Dim tmpItem As ListViewItem

If chkOnlyExternal.Checked = True Then

'Remove internal users.

For Each lvitem As ListViewItem In lstUsers.Items

If db.IsInternalUser(lvitem.Tag.ToString) Then

tmpItem = lvitem

lvCollection.Add(lvitem, lvItem.Tag.ToString)

lvitem.Remove()

rmCount += 1

End If

Next

MessageBox.Show("Removed from listview: " & rmCount.ToString)

Else

MessageBox.Show("Collection count: " & lvCollection.Count.ToString)

If lvCollection.Count > 0 Then

For Each lvItem As ListViewItem In lvCollection

lstUsers.Items.Add(lvItem)

lvCollection.Remove(lvItem.Tag.ToString)

addCount += 1

Next

MessageBox.Show("Added back to listview: " & addCount.ToString)

End If

End If

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Martin Schulze | last post by:
Hello, i tried to compose myself a custom usercontrol which is derieved from System.Windows.Forms.UserControl. It contains 2 comboboxes and one textbox (which are also custom controls, but...
10
by: Bonj | last post by:
Hello. I hope somebody can help me on this, because I'm running out of options to turn to. I have almost solved my regular expression function. Basically it works OK if unicode is defined. It...
1
by: Chris | last post by:
Hi all, I posted the following in microsoft.public.dotnet.framework.windowsforms but it seems that group has little traffic. Hi all, I have a listview box which is populated from methods of...
7
by: Laurent Navarro | last post by:
Hello, I'm creating a control including a listview. I would like to be able to set the columns through the Design Tool in Visual Studio, so I created a parameter to access the columns of my...
5
by: John Devlon | last post by:
Hi, Does anyone know how to get a value of a second column of a selected item in Listview. I've create a listview and added this code Listview.Items.Clear() Listview.Columns.Clear()...
1
by: samoore33 | last post by:
I found this code on MSDN, and it works great. It creates a ListView dynamically and add items to it and all. It is great. I have changed a few of the column names to suit me. Dim listView1 As...
4
by: forest demon | last post by:
I have an IList/Collection that contains items in a ListView. If i click on an item in the ListView, i can capture the index (lv.SelectedItems.Index) and reference the correct item in the...
3
by: RT | last post by:
Is there any way to make Listview items invisible or otherwise keep them from displaying? Seems like temporarily removing, then restoring later would be a severe runtime hit.
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.