?
Solved

VB 2005 Delete all rows selected in a ListView

Posted on 2007-11-19
9
Medium Priority
?
752 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 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:Murray Brown
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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Author Comment

by:Murray Brown
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:Murray Brown
ID: 20317939
Thanks very much
0
 

Author Comment

by:Murray Brown
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:Murray Brown
ID: 20318044
Great. That worked. Thanks very much!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

771 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