How to add a key to a listview item in VB.NET

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?

    Requires Free Membership to View

    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

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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.

This was first published in January 2005