0

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.

2 Answers 2

6

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);
          }
      }
}
7
  • 1
    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 Jun 30, 2009 at 14:26
  • 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
    Jun 30, 2009 at 14:28
  • OP didn't specify C#3.0, so he may need to declare "User user = myListBox.SelectedItem as User;" Jun 30, 2009 at 14:28
  • 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
    Jun 30, 2009 at 14:29
  • 1
    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
    Jun 30, 2009 at 14:31
2

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);
4
  • what is this syntax? it can detect members by string??
    – Nefzen
    Jun 30, 2009 at 14:39
  • if I change the users Dictionary, it won't change the listBox though, huh? that's not that good :\
    – Nefzen
    Jun 30, 2009 at 14:49
  • 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
    Jun 30, 2009 at 14:54
  • Anyway if you adding items to listbox via Items.Add list remain unchanged if you add or remove items from your dictionary.
    – arbiter
    Jun 30, 2009 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.