Sign in to follow this  
slayemin

C# Listbox question

Recommended Posts

I'm trying to write an app in C#. I'm trying to present a list box which contains a list of text strings and a list of hidden index values.

I've got something like this in code:

Dictionary<Guid, MyObject> data; //filled with stuff

ListBox myListbox = new ListBox();

foreach(KeyValuePair<Guid, MyObject> kvp in data)
{
myListbox.Items.Add(kvp);
}


The problem is that the Items collection is a list of generic objects and C# tries its best to print out the object. I really want the Key to be the hidden value and the Value to be the displayed list item. However, if I just display the value, I lose the key and can't index into my Dictionary data structure.

Ideally, there should be an overloaded function on the Listbox.Items.Add() method which accepts a display value and a hidden value. Alas, I'm dumb and stuck to hacks and inelegant trickery to get around this.

I've come up with the ugliest, most inelegant hack to get around this (almost worthy of DailyWTF). It involves creating a helper class to be a container for two strings, an array list since it can be datasourced, setting the listbox datasource to the array list, and setting the DisplayMember and ValueMembers to the helper class strings. It works but I'm so disgusted by this garbage that I'm begging for help. What's the right way to do this?

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Did you try using the ListBox's DisplayMember property?


Yes. I think it's necessary when you're setting the datasource for the list box.

Here is my exact code:

//class definition
public class StringPair
{
string str_value;
string str_display;

public StringPair(string Value, string Display)
{
str_value = Value;
str_display = Display;
}

public string Value
{
get
{
return str_value;
}
}

public string Display
{
get
{
return str_display;
}
}
}



//poll the database for a list of all the sets
list_existing_sets.Items.Clear();

ArrayList TempArray = new ArrayList();
foreach(KeyValuePair<Guid, ItemSet> set in GameDB.ItemSetDB)
{
TempArray.Add(new StringPair(set.Key.ToString(), set.Value.SetName));
}

list_existing_sets.DataSource = TempArray;
list_existing_sets.DisplayMember = "Display";
list_existing_sets.ValueMember = "Value";
list_existing_sets.ClearSelected();

Share this post


Link to post
Share on other sites
I think you want to set the DisplayMember property before setting the DataSource property. And you should definitely change ArrayList to List<StringPair>, since ArrayList contains objects for elements afaik, which natively don't expose the properties that your actual StringPairs have, but which you need.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
So what went wrong using the DisplayMember with the KeyValuePair?


This all works, but I feel it's bad and there's probably a better way to do it.
1. I'd rather not have to create a StringPair class just to get a list box working.
2. I'd rather not use an additional ArrayList in such an unneccessary fashion (nitpick on elegance)
3. If I use the datasource method, I can't change the contents of my listbox (no inserts and deletes) -- this is the worst drawback.


I'm tempted to just store the string values in the list box and perform a string comparison on the elements in the dictionary when I need to find the selected object. I'm sure that'll also work, but then I can't have objects with duplicate text strings and the search will be linear instead of constant time.

Share this post


Link to post
Share on other sites
Quote:
Original post by slayemin
This all works, but I feel it's bad and there's probably a better way to do it.

I think you didn't read what I wrote. Let's try this again with emphasis:
Quote:
So what went wrong using the DisplayMember with the KeyValuePair?

I wasn't asking about with your StringPair object, or with an ArrayList or with a DataSource. I was asking when adding the original KeyValuePairs into the listbox.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by slayemin
This all works, but I feel it's bad and there's probably a better way to do it.

I think you didn't read what I wrote. Let's try this again with emphasis:
Quote:
So what went wrong using the DisplayMember with the KeyValuePair?

I wasn't asking about with your StringPair object, or with an ArrayList or with a DataSource. I was asking when adding the original KeyValuePairs into the listbox.


You're right. I apologize for not reading carefully enough. I'd been coding for 12+ hours and was too tired to think coherently. Anyways, I slept on it and thought about what you just said and tried something else...and it worked in the first five minutes! thank you, thank you, thank you so much.

I was originally setting the DisplayMember and ValueMember to the actual variable names (Guid, and ItemSet) which wasn't working. I should have been setting them to "Key" and "Value" instead.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this