[Solved] add only one property from each item to listbox [closed]


Your attempt at @SLaks suggestion was close, but you can’t add the list of users to the listbox in that manner. This should get you closer:

protected void Button1_Click(object sender, EventArgs e)
{
    users.Add(new User { connectionid = TextBox1.Text, nick = TextBox2.Text });
    foreach (User u in users) {
        lbUsers.Items.Add(new ListItem(u.nick, u.connectionid));
    }
}

To Add to the Items collection of a ListBox requires the item to be a ListItem. You can instantiate a new one inline and pass the string/value pair that you want associated with it.

0

solved add only one property from each item to listbox [closed]