ListView Control

Adding and removing items

Adding an item to the ListView control is very similar to adding items to a ListBox control, however you have much more options. The following code, simply adds a few items, with no icons to ListView1:

ListView1.ListItems.Add , ,"Fred"
ListView1.ListItems.Add , ,"Sarah"
ListView1.ListItems.Add , ,"Paul"

You will notice that I have missed two parameters before specifying the text. The first specifies the Index (a unique number) to that item. If you leave that option blank, the ListView will automattically add a unique Index. The second parameter specifies a key. This is a unique string that can also be used to identify that item. If left blank, no key will be assigned.

To remove an item, simply use this syntax. Replace 1 with its index or the items key.

ListView1.ListItems.Remove(1) '// removes the first item

So, to remove the selected item, you would use:

ListView1.ListItems.Remove(ListView1.SelectedItem) '// removes the selected item

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”