Windows Develop Bookmark and Share   
 index > Windows Forms General > Key-Based List Control?
 

Key-Based List Control?

Hi Everyone,

I was just curious. Is there a Windows Forms List control out there that is "key-based". Basically, I want to display all of my Dictionary items in a List on a form, with the key of each item of the Dictionary being how I access each item in the List.

I've looked at the ListView and ListBox, but it doesn't look like either one would do the trick.

Thanks in advance
Sweeps78  Thursday, November 30, 2006 5:23 PM

In you class, define a ToString method which displays the object as you'd like it to appear in the listbox. Then just store a collection of the objects in the ListBox.

James Curran  Thursday, November 30, 2006 5:57 PM
Yes, that part is easy. What I want to be able to do is remove an item from a listbox/listview by a unique key. I see that the ListView control has a unique key accessor, which is assigned using one of two of the overloaded ListView.Items.Add() methods:

ListView.Items.Add(string key, string text, string imageKey)

or

ListView.Items.Add(string key, string text, string imageIndex)

However, there isn't an overload that allows me to assign a unique key to an item I would like to add using a ListViewItem object:

ListViewItem lvi = new ListViewItem(SerialNumber.ToString()); lvi.SubItems.Add(Name);
lvi.SubItems.Add(Type().ToString());
ListView.Items.Add(lvi); <- No overload that allows me to assign a key to this ListViewItem

Hopefully this gives you a better understanding to what I am trying to accomplish.
Sweeps78  Thursday, November 30, 2006 6:10 PM

Read the remarks for the Add method : "The Name property corresponds to the key for a ListViewItem in the ListView.ListViewItemCollection." In other words:

ListViewItem lvi = new ListViewItem(SerialNumber.ToString());
lvi.SubItems.Add(Name);
lvi.SubItems.Add(Type().ToString());
lvi.Name = key;
ListView.Items.Add(lvi);

James Curran  Thursday, November 30, 2006 8:11 PM
whoops

Thank you yet again James!
Sweeps78  Thursday, November 30, 2006 10:11 PM

You can use google to search for other answers

Custom Search

More Threads

• The age old Visual Inheritance (VI) problem
• graphics.transform.reset doesn't do anything in a picturebox ?
• Black background appearing in readonly textbox for a application in Vista Machine
• DefaultCredentials = null
• How to reclaim ToolBarTray space ?
• Scroll Wheel Problem
• File picker/file browser control
• Window class name is not valid.
• Inherited TextBox VS 2005 crashes
• Trying to create an Managed ActiveX control that can be instantiated by unmanaged code