VB.Net ListView Key

In Visual Basic 2008, there is no 'key' while adding an ListViewItem to ListView. Unlike in Visual Basic 6 (98), you can associate a key to a ListViewItem.

You wanted to use the the following methods, but don't know where to associate a key to the ListViewItem:
  • ListViewItem.ListViewSubItemCollection.IndexOfKey
  • ListViewItem.ListViewSubItemCollection.Find
  • ListViewItem.ListViewSubItemCollection.ContainsKey
  • ListviewItem.ListViewSubItemCollection.RemoveByKey

MSDN doesn't explain well enough.

The 'key' is actually the 'name' property of each ListViewItem's subitem. And each subitem can have a name (key).

And you are able to find listViewItem by specifying either one of the name (key) of the subitems by using ListViewItem.ListViewSubItemCollection.IndexOfKey method. This is powerful. And if there are duplicate names(keys), this method will return with the first ListViewItem. Otherwise, just use the the ListViewItem.ListViewSubItemCollection.Find method which will return you with an array consists of ListViewItems which match the name (key).

This means that now you can have multiple keys in a ListViewItem. This is even more powerful.

Download the source file (VB 2008).

In this sample, a ListView will serve as a display grid for objects in a collection. Each associated object (in collection) and ListViewItem (in ListView) will share the same key (generated unique key).

Thanks to this link for heads-up.