Part One on performing various Listview Tasks using VB.NET, VB 2008, VB 2010, and Visual Basic 2013.
The listview control has been available to VBers since the classic days. VB.NET continues with its version of the listview control as well. The control can provide user-friendly listing features to your programs. You can add Groups, Columns, and more with various display preferences like Icons and Details. The purpose of this post is simply to give you some basic code that i’ve had to use over time for removing items from the Listview Control.
Note: These codes should have no problems working with Visual Basic.NET 2008, Visual Basic 2010, and newer. For this article I am calling my Listview control’s name: Listview1. So all of the code will be using that name when referring to the listview object. I also set the controls ‘View’ property to: “Details” and added 2 columns.
Removing/Deleting Listview Items…
I have three tasks related to item removal. Removing All, Remove Checkmarked, and Remove the Selected items. Once you have a basic understanding of the component these task are quite easy. It actually takes very little code.
Remove – All Items
Remove – Checkmarked Items Only
For item As Integer = 0 To Listview1.CheckedItems.Count - 1 Listview1.CheckedItems(0).Remove() Next
Do While Listview1.CheckedItems.Count > 0 Listview1.CheckedItems(0).Remove() Loop
Remove – Selected Items Only
For item As Integer = 0 To Listview1.SelectedItems.Count - 1 Listview1.SelectedItems(0).Remove() Next
Do While Listview1.SelectedItems.Count > 0 Listview1.SelectedItems(0).Remove() Loop
Thats all there is to it! If I come up with more ways or come across any other codes related to removing items then I will update this post. In the next article I plan on showing how to Sort items in the Listview control and Shuffle, or Randomize the contents/items of the Listview control. Have fun!
Minor Revision: 2015