请问在winform结构下如何向listbox中添加一个item,既可访问它的text也可以访问它的value

uncleAndyChen 2004-11-04 09:25:51
如上,谢谢
...全文
144 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yxred 2004-11-04
  • 打赏
  • 举报
回复
这种的用数据绑定最好不过了,如果想手动添加,同上
langmafeng 2004-11-04
  • 打赏
  • 举报
回复
public struct TestSource
{
private int val;
private string display;

public TestSource(int val, string display)
{
this.val = val;
this.display = display;
}
public int Val
{
get{return this.val;}
}
public string Display
{
get{return this.display;}
}
}

public class MyTest
{
new TestSource[] testSource = new TestSource[]{ new TestSource(0, "AAA"), new TestSource(1, "BBB"), new TestSource(2, "CCC"),
new TestSource(3, "DDD"),
new TestSource(4, "EEE"),
new TestSource(5, "FFF"),
new TestSource(6, "GGG"),
new TestSource(7, "HHH"),
new TestSource(8, "III"),
new TestSource(9, "JJJ")
};

this.listBox.DisplayMember = "Display";
this.listBox.ValueMember = "Val";
this.listBox.DataSource = testSource;
}
wb00422 2004-11-04
  • 打赏
  • 举报
回复
先定义一个struct结构体
里面有一个text和Value属性,同时为了使text能显示出来,重写ToString()方法,在里面输出Text

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧