473,847 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 34806
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.L istViewItem eachItem in
this.listView1. SelectedItems)
{
this.listView1. Items.Remove(ea chItem);
}

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(ListVie wItem item in listview.Select edItem)
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(ListVie wItem item in listview.Select edItem)
elems.Add( item );

foreach(ListVie wItem 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******** ******@TK2MSFTN GP10.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.c om> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.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.L istViewItem eachItem in
this.listView1. SelectedItems)
{
this.listView1. Items.Remove(ea chItem);
}

Nov 17 '05 #4
"Ignacio Machin ( .NET/ C# MVP )" wrote...
ArrayList elems = new ArrayList()
foreach(ListVie wItem item in listview.Select edItem)
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.Select edItems );

foreach(ListVie wItem 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**********@D oNotSpam.hotmai l.com> wrote in message
news:OQ******** ******@tk2msftn gp13.phx.gbl...
"Ignacio Machin ( .NET/ C# MVP )" wrote...
ArrayList elems = new ArrayList()
foreach(ListVie wItem item in listview.Select edItem)
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.Select edItems );

foreach(ListVie wItem 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.c om> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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
3118
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 list box. The problem is that if there are many items selected (thousands), then removing the items from the right side list box takes for ever because we are removing them one at a time, which causes the listbox to re-index everything before we...
7
24127
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 so using item index is not possible. To get the list of selected items I use: ListView.SelectedListViewItemCollection selItems = this.listView1.SelectedItems;
10
1709
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 the following syntax but keep getting the error "Specified cast is not valid". I don't know what's causing this ? Can anybody help ? For Each Item As ListViewItem In frmRepParams.lstAnalysis.SelectedItems If Len(strAnalysis) = 0 Then
3
8330
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 would be expected based on the visual appearance. Is there any way to force the selected items to be highlighted even when the control does not have focus? Thanks, Lance
0
7548
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 view in .NET 2.0 with multiple selection turned on... since the .NET documentation is EXTREAML vague on how to do this and offers no clue on what the virtual mode events do or how to use them... just thought this might help someone else out...
5
10093
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 because the ListView seems to be trying to "re-select" items that are no longer there! for example, giventhat I have 3 items in my list: Select the first and remove it - no problem.
1
12858
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 listview items but I cannot iterate through all selected items. the listview only has a selectedItem property and not selectedItems. How do i iterate through the selected listview items? Garudzo
1
2995
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
2513
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 'Clear listbox contents. 'Me.FileList.RowSource = "" 'Set up the File Dialog.
0
9730
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
10983
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
10706
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
10338
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...
1
7882
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5719
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...
0
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4528
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
4119
muto222
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.