Solved

VB 2005 Delete all rows selected in a ListView

Posted on 2007-11-19
9
747 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
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.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

19 Experts available now in Live!

Get 1:1 Help Now