views:

116

answers:

2

I have this situation:

Dictionary<Int32, userInfo> users = new Dictionary<Int32, userInfo>();

Where the Int32 is the user id and userInfo contains the name and other details. I want to display a list of names at the form, and allow the person using the program to double click on one to open a window (similar to what happens in MSN messenger). When a user's name is double clicked I need to know the id, so I can do

OpenChatWindow(Int32 id, string name);

I am really not sure how to do it, I am using winforms. I thought of ListBox, but again, I'm not sure how. what should I do?

PS. userInfo.name is unique in the Dictionary, but I really don't want to start looking for it.

+6  A: 

I wouldn't chose to store user information in a dictionary object. I would create a class called User and store all the info, incl the id, in the class.

Override the ".ToString()" method of the User class and make it output the name of the User instead i.e.

protected override string ToString()
{
     return this.Name;
}

Then you can directly add your User objects to the List box and when they are added the name of the users will be displayed. You may have to cast them as Objects I am not 100% if they are casted automatically...I will get back to you on that!

Then on the SelectedIndexChanged event of the ListBox, you can simply do:

var user = myListBox.SelectedItem as User;

Which will return the User object for you to use for your lookup:

OpenChatWindow(user.ID, user.Name);

So if you have a controller class you could hook it up to the SelectedIndexChanged event of the List box then process what it is you want to do:

public void OnUserSelected(object sender, EventArgs e)
{
      var lstBox = sender as ListBox;
      if (lstBox != null)
      {
          if (lstBox.SelectedItem is User)
          {
               var user = lstBox.SelectedItem as User;
               OpenChatWindow(user.ID, user.Name);
          }
      }
}
James
There's no need to cast the Users as Objects when adding them to the ListBox, implicit downcasting to Object is always allowed in .NET
Patrick McDonald
I was about 99% sure Patrick thanks for clearing it up! lol I am pretty sure you need to do a cast tho if you use the AddRange method?
James
OP didn't specify C#3.0, so he may need to declare "User user = myListBox.SelectedItem as User;"
Patrick McDonald
Elegant solution. I am not saving a user information in the dictionary, I am saving a lot of users' information in the dictionary, in such a class. I don't think this method allows me to save the id too, but I guess I can add it to the userInfo class.
Nefzen
I think I would throw away that lstBox != null check, because that only guards you against a potential exception, but it's still a faulty behaviour. If your lstBox is null when you expect it not to be you have a bigger problem. Anyway, +1 because this is the standard way to go.
Skurmedel
Would it not be a better option to create a class which stored all the user information then maintain a list of User objects rather than using the dictionary approach?
James
@Skurmedel...better safe than sorry!
James
+2  A: 

Change userInfo class this way so it will include userId (if it does not have it now). Then you can databind dictionary to listbox:

listBox.DisplayMember = "Name";
listBox.ValueMember = "userId";
listBox.DataSource = new List<UserInfoClass>(users.Values);

Then you can get simply cast SelectedItem property to your userInfo class:

UserInfoClass userInfo = (UserInfoClass)listBox.SelectedItem;
OpenChatWindow(userInfo.userId, userInfo.Name);
arbiter
what is this syntax? it can detect members by string??
Nefzen
if I change the users Dictionary, it won't change the listBox though, huh? that's not that good :\
Nefzen
With this syntax listbox extract values automatically, read more in msdn. What according to dictionary, if you do not need quick lookup, then you can replace Dictionary with List<UserInfoClass> and bind it directly to listBox.
arbiter
Anyway if you adding items to listbox via Items.Add list remain unchanged if you add or remove items from your dictionary.
arbiter