Solved

VB 2005 Delete all rows selected in a ListView

Posted on 2007-11-19
9
748 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:murbro
ID: 20317929
Just tested both. You were right. Thanks very much and sorry about the confusion
0
 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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