473,224 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Q: Removing selected items from a ListView?

Hi!

I'v been struggeling with removing selected items from a listview.

Anyone that can give me a piece of code that does this?

I am a newbee to this C# and cant figure it out....

Regards
Martin Arvidsson
Nov 17 '05 #1
7 34706
This should do the trick. It just loops through the ListView controls
SelectedItems collection and passes that item to its Remove method.

foreach(System.Windows.Forms.ListViewItem eachItem in
this.listView1.SelectedItems)
{
this.listView1.Items.Remove(eachItem);
}

Nov 17 '05 #2
Hi,

Well, The below code does work for a DataTable, I bet it will work for a
listview as you will have the exact same issues.

The problem is that you cannot do this:
foreach(ListViewItem item in listview.SelectedItem)
listview.Items.Delete( item );
cause you are modifying the collection and iterating at the same time ( an
exception will be throw IIRC ).

Solution:
Use a temp list
ArrayList elems = new ArrayList()
foreach(ListViewItem item in listview.SelectedItem)
elems.Add( item );

foreach(ListViewItem item in elems)
listview.Items.Delete( item );


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.net> wrote in
message news:OQ**************@TK2MSFTNGP10.phx.gbl...
Hi!

I'v been struggeling with removing selected items from a listview.

Anyone that can give me a piece of code that does this?

I am a newbee to this C# and cant figure it out....

Regards
Martin Arvidsson

Nov 17 '05 #3
Hi,

IMO this will not work, you are modifying a collection at the same time you
are iterating it. if you delete an item then that item is not longer
selected so the collection change and the iteration fails
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"gmiley" <gm****@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
This should do the trick. It just loops through the ListView controls
SelectedItems collection and passes that item to its Remove method.

foreach(System.Windows.Forms.ListViewItem eachItem in
this.listView1.SelectedItems)
{
this.listView1.Items.Remove(eachItem);
}

Nov 17 '05 #4
"Ignacio Machin ( .NET/ C# MVP )" wrote...
ArrayList elems = new ArrayList()
foreach(ListViewItem item in listview.SelectedItem)
elems.Add( item );
It seems overkill to iterate through the items to copy the references to
another list. Shouldn't this work just as well?

ArrayList elems =
new ArrayList( listview.SelectedItems );

foreach(ListViewItem item in elems)
listview.Items.Delete( item );

// Bjorn A
Nov 17 '05 #5
It works fine, SelectedItems is a seperate collection. It does not
remove the item from the SelectedItems collection, it removes it from
the Items collection.

Nov 17 '05 #6
Hi ,

It should be the same, only that it's the constructor of ArrayList where
the iteration occurs

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:OQ**************@tk2msftngp13.phx.gbl...
"Ignacio Machin ( .NET/ C# MVP )" wrote...
ArrayList elems = new ArrayList()
foreach(ListViewItem item in listview.SelectedItem)
elems.Add( item );


It seems overkill to iterate through the items to copy the references to
another list. Shouldn't this work just as well?

ArrayList elems =
new ArrayList( listview.SelectedItems );

foreach(ListViewItem item in elems)
listview.Items.Delete( item );

// Bjorn A

Nov 17 '05 #7
Hi,

Yes, but I bet that the selectedItem collection is modified depending of
the Items collection, there should be a method in ListView that hook in the
Add/Delete, etc events of Items, when It does change it will most probably
recreate the Selected collection iterating in the Items collection and
checking for the Selected property.

Again, I have not tested this, a simple test shoudl say if the above is
correct or not, It does happens with a DataTable / DataView , beside the
above algorithm makes perfectly sense.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"gmiley" <gm****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
It works fine, SelectedItems is a seperate collection. It does not
remove the item from the SelectedItems collection, it removes it from
the Items collection.

Nov 17 '05 #8

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

Similar topics

3
by: Jeremy Owens-Boggs | last post by:
We are trying to implement a dual list box selection where you have two list boxes, You highlight items in the right side list box, click a button and this moves those items over to the left hand...
7
by: GTi | last post by:
I have a listview that with selected items. But I want to "reselect" items after a refresh. Each items have a uniqe value in the Tag object. After a refresh some new items may be added or removed...
10
by: Robert Zirpolo | last post by:
I have a listview in my application which now enables multiple entries to be selected. I now need to put the value of the selected entries into a string to be used for reporting purposes. I use...
3
by: ljlevend2 | last post by:
I've noticed that selected items in a ListView are only highlighted if the control has focus. I find this to be highly undesirable because it can cause the control to behave differently than what...
0
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list...
5
by: Phill W. | last post by:
(VB'2003) What's the correct way to remove multiple, selected items from a ListView control (say, from a ContextMenu)? I ask because I'm getting a very annoying ArgumentOutOfRangeException...
1
by: Garudzo | last post by:
Hi all I am developning a small application in MS Access 2003 on a windows XP platform. I am using windows treeview and Listview controls. I have discovered that I can do a multi select of...
1
by: asappradeep | last post by:
Dear friends I am using VBA language i am using Combobox1.RemoveItem(Combobox1.Listindex) fro removing the selected item but it is removing first item in the list pls give the solutiojn
7
by: xraive | last post by:
Currently I only get the file path. Is there way to retrieve the file name or do I have to just use the split function. Dim fDialog As Office.FileDialog Dim varFile As Variant ...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.