Key-Based List Control?  
Author Message
Sweeps78





PostPosted: Windows Forms General, Key-Based List Control? Top

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



Windows Forms3  
 
 
James Curran





PostPosted: Windows Forms General, Key-Based List Control? Top

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.



 
 
Sweeps78





PostPosted: Windows Forms General, Key-Based List Control? Top

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.


 
 
James Curran





PostPosted: Windows Forms General, Key-Based List Control? Top

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);



 
 
Sweeps78





PostPosted: Windows Forms General, Key-Based List Control? Top

whoops

Thank you yet again James!