C#中添加对象以它的对象列表框,并显示字符串 [英] c# add object to listbox and show string of object in it

查看:412
本文介绍了C#中添加对象以它的对象列表框,并显示字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用一个列表框和对象添加到它。



对象包含2个变量,可以说用户名和用户ID。



如何添加列表中的像
listbox.Items.add(对象)的对象;
,并显示该对象?



日Thnx!


解决方案

您可以使用的DisplayMember 和<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.valuemember.aspx> ValueMember 属性来显示用户名属性,并使用它的用户ID 属性作为列表项的值:

 类YourItem 
{
公共字符串用户名{获得;组; }
公共字符串userid {搞定;组; }
}

yourListBox.DisplayMember =用户名;
yourListBox.ValueMember =用户ID;

yourListBox.Items.Add(新YourItem {
用户名=FooName,
用户ID =FooId
});


Im using a listbox and adding objects to it.

the object contains 2 variables, lets say username and userid.

how can i add the objects in the list like listbox.Items.add(object); and show the username of that object?

thnx!

解决方案

You can use the DisplayMember and ValueMember properties to display the UserName property of your object and use its UserId property as the list item value:

class YourItem
{
    public string UserName { get; set; }
    public string UserId { get; set; }
}

yourListBox.DisplayMember = "UserName";
yourListBox.ValueMember = "UserId";

yourListBox.Items.Add(new YourItem {
    UserName = "FooName",
    UserId = "FooId"
});

这篇关于C#中添加对象以它的对象列表框,并显示字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆