473,499 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing indices from a List and ListView

HaLo2FrEeEk
404 Contributor
I've got a List and a ListView. The List stores a bunch of information and the ListView displays some of it. I'm trying to add a context menu so that when I select item(s) in the ListView I can either save or remove them. As long as I haven't removed anything, it works great, as soon as I remove something my indexes get messed up. How I'm doing it is doing a foreach loop through the ListView.SelectedIndices and doing List.RemoveAt(index) and ListView.RemoveAt(index).

Is there a way I can remove those items without my indexes getting messed up?

I think basically what's happening is, I select item 1 and 2, indices 0 and 1. It goes through the loop and removes the item at index 0, which makes the item at index 1 turn into index 0, then it removes the item at index 1, which is actually the original item at index 2. So in essence I'm removing items 1 and 3 (indices 0 and 2). I have a feeling if I do it backwards, starting with the largest selected index, I won't have this problem, but I can't seem to find a way to operate in reverse. I tried:


ListView.SelectedIndexCollection selected = fileList.SelectedIndices;

Then doing Array.Reverse(selected), but selected isn't an array. I also tried to see if SelectedIndexCollection had some method to reverse...it doesn't.

Can someone help me? Also, if I'm wrong and doing it in reverse won't help, can you suggest a way I might go about it?
Jun 14 '10 #1
0 1076

Sign in to post your reply or Sign up for a free account.

Similar topics

9
1447
by: Lol McBride | last post by:
Hi All, I'm looking for some help in developing a way to test for the uniqueness of an item in a large list.To illustrate what I mean the code below is an indicator of what I want to do but I'm...
9
4039
by: Goh, Yong Kwang | last post by:
I'm currently doing a project that has a array that supposed to be determined only at runtime. Originally, the prototype I did as a proof of theory (or a quick hack) So one method is to create a...
3
9281
by: MikeY | last post by:
Hopefully someone can help me on this. I am using C#, making Windows forms. I have created a listView with checkbox's. I have enabled the checkboxes under the properties, and all the data,...
0
2223
by: Chris Putnam | last post by:
I am working on an application that includes a feature where a directory is spidered and thumbnails are displayed. I have it pretty much down, but when the bitmaps are loaded from file, memory is...
3
2800
by: Progalex | last post by:
Hi! I have this question: I have a listview which gets populated with some file names. At the moment I have implemented d&d, but I'd like also to be able to move up and down items inside my...
2
1927
by: Curious | last post by:
Hi, I am removing problems but I am having a problem. The code below is the code being used to remove items from the listview. ListViewItem oldItem = listViewAllStates.SelectedItems;...
3
15343
by: Chris | last post by:
Is it possible to hide specific rows in a listview? I currently have a listview in details view and the user can select multiple views which shows the listview items matching the selected...
1
3395
by: beginner | last post by:
Hi All, I am wondering if there is any way to handle exceptions inside list comprehension. For example, I want to skip the point if f(x) raises an exception. How can I do that without...
1
2645
by: Andi | last post by:
Hi, I have added a soring icon to a ListView column header when the user clicks on it. I want to remove this icon, when the user clicks on another column by setting the image index to -1. That...
0
1229
by: sanndeb | last post by:
Hi i found this article to build nested lists.... http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html But the problem is now i don't know how to enable the inner list for...
0
7134
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
7180
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,...
0
7395
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...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3108
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...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
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...

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.