473,888 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
+ 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.IsInternalUs er(lvitem.Tag.T oString) Then

tmpItem = lvitem

lvCollection.Ad d(lvitem, lvItem.Tag.ToSt ring)

lvitem.Remove()

rmCount += 1

End If

Next

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

Else

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

If lvCollection.Co unt > 0 Then

For Each lvItem As ListViewItem In lvCollection

lstUsers.Items. Add(lvItem)

lvCollection.Re move(lvItem.Tag .ToString)

addCount += 1

Next

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

End If

End If


Nov 21 '05 #1
3 2158
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***@spamform e.com> wrote in message
news:eM******** ******@TK2MSFTN GP15.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.IsInternalUs er(lvitem.Tag.T oString) Then

tmpItem = lvitem

lvCollection.Ad d(lvitem, lvItem.Tag.ToSt ring)

lvitem.Remove()

rmCount += 1

End If

Next

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

Else

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

If lvCollection.Co unt > 0 Then

For Each lvItem As ListViewItem In lvCollection

lstUsers.Items. Add(lvItem)

lvCollection.Re move(lvItem.Tag .ToString)

addCount += 1

Next

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

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******** ******@tk2msftn gp13.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***@spamform e.com> wrote in message
news:eM******** ******@TK2MSFTN GP15.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.IsInternalUs er(lvitem.Tag.T oString) Then

tmpItem = lvitem

lvCollection.Ad d(lvitem, lvItem.Tag.ToSt ring)

lvitem.Remove()

rmCount += 1

End If

Next

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

Else

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

If lvCollection.Co unt > 0 Then

For Each lvItem As ListViewItem In lvCollection

lstUsers.Items. Add(lvItem)

lvCollection.Re move(lvItem.Tag .ToString)

addCount += 1

Next

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

End If

End If


Nov 21 '05 #3
psh... you kids these days at Reschini and your crazy ideas :P
"Matt Michael" <ih***@spamform e.com> wrote in message
news:eM******** ******@TK2MSFTN GP15.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.IsInternalUs er(lvitem.Tag.T oString) Then

tmpItem = lvitem

lvCollection.Ad d(lvitem, lvItem.Tag.ToSt ring)

lvitem.Remove()

rmCount += 1

End If

Next

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

Else

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

If lvCollection.Co unt > 0 Then

For Each lvItem As ListViewItem In lvCollection

lstUsers.Items. Add(lvItem)

lvCollection.Re move(lvItem.Tag .ToString)

addCount += 1

Next

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

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
12509
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 directly derived from the .net classes ComboBox and TextBox) I did the custom control in the visual studio .net designer in which i
10
1855
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 doesn't work OK in ANSI mode however, as it has to use MultiByteToWideChar and WideCharToMultiByte. I've discovered that the regular expression part is working fine. As far as I can tell the regular expression code is correctly parsing what it...
1
2865
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 objects stored in a List<T> collection. The items in this collection are generated from a user filled out form. The items in the listview have the same index as the ones in List<T>, so I
7
3561
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 listview: private ListView.ColumnHeaderCollection myColumns { get
5
25978
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() Listview.View = View.Details
1
2063
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 New ListView listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200)) ' Set the view to show details.
4
13167
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 associated IList/Collection . Once I've sorted the ListView, obviously the indices do not match up anymore. So, should i sort the IList/Collection when I sort the ListView (which seems overkill to me) or some how sort the indices associated with the...
3
7652
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
2205
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 problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
0
9961
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9800
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11182
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10886
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5824
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4245
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.