How to add a key to a listview item in VB.NET
How can I add a key to a listview item in VB.NET?
When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.
Hannah Smalltree, Editorial Director
Unfortunately, the ListView control in .NET v1.x doesn't support indexing of its items collection by key. This has been added in .NET v2. For now, you'll have to inherit the control and extend it to support this, by exposing additional methods/properties that work with keyed items. You can for example expose a KeyedItems property where you allow the addition of items with a key. When something is added with a key, you simply store the item in the base class Items collection, but keep the index in a hashtable, for example. The property you expose would have to expose the same interface (plus the keyed members) as the Items one, so you'll probably be better off inheriting from ListView.ListViewItemCollection.
Once you have your custom control (class), it's just a matter of adding it to the toolbox manually or just doing a find-and-replace to change the instances you already have.
Dig Deeper
-
People who read this also read...
This was first published in January 2005