Solved

VB 2005 Delete all rows selected in a ListView

Posted on 2007-11-19
9
746 Views
Last Modified: 2013-11-26
Hi

In Visual Studio 2005 VB, how would you delete all the rows selected in a ListView where multiselect is true
0
Comment
Question by:murbro
  • 5
  • 4
9 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20317888
For Each item as ListViewItem in somelistview.SelectedItems
      somelistview.Remove(item)
Next
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20317896
I think should be instead:

For Each item as ListViewItem in somelistview.SelectedItems
      somelistview.Items.Remove(item)
Next
0
 

Author Comment

by:murbro
ID: 20317897
Hi

Looking around the net I found this example, posted by angelII, which is better?
If you want to remove the items from the list, change the loop, because for each will fail if you remove items:

dim l as long
for l = ListView.ListItems.Count - 1 to 0
  if ListView.Listitems(l).Selected then
    ListView.Listitems.Remove l
  end if
next
0
 

Author Comment

by:murbro
ID: 20317929
Just tested both. You were right. Thanks very much and sorry about the confusion
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20317934
You can do it this way, to avoid fail:

Dim items as Array = new Array(somelistview.SelectedItems.Count)
somelistview.SelectedItems.CopyTo(items,0)
For Each item as ListViewItem in items
      somelistview.Items.Remove(item)
Next
0
 

Author Comment

by:murbro
ID: 20317939
Thanks very much
0
 

Author Comment

by:murbro
ID: 20317947
I am getting an error on the "New Array" saying
" 'New' cannot be used on a class that is declared 'mustinherit' "
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20317971
Sorry, C# customs. Try with:

Dim items(somelistview.SelectedItems.Count-1) As Object
0
 

Author Comment

by:murbro
ID: 20318044
Great. That worked. Thanks very much!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now