简体   繁体   English

添加项目列表框

[英]Add item listbox

How can I make a listBox in C# where the item is a object with 3 variables(2 strings,1 int)?The text would be one of the Strings while the Value would be 1 string and 1 int. 我如何在C#中创建一个列表框,其中该项目是一个具有3个变量(2个字符串,1个整数)的对象?文本将是字符串之一,而值将是1个字符串和1个整数。 I saw how to make with 2 variables here: c# add object to listbox and show string of object in it 我在这里看到了如何使用2个变量制作代码: c#将对象添加到列表框并在其中显示对象字符串

The has object has 有对象有

 class myItem
    {
        int nSomething;
        String name;
        String tMethod;

        public myItem(String name,int nSomehting,String tMethod)
        {
            this.nSomething = nSomething;
            this.name = name;
            this.tMethod = tMethod;
        }
    }

If I set: 如果我设置:

listbox.DisplayMember = "name";
listbox.ValueMember = "nSomething";

would I be able to get "tMethod" with this? 我可以得到“ tMethod”吗?

listbox.SelectedItem.tMethod

Also,when I add the item for the listbox,how can i make sure its unique(just the name is enough)? 另外,当我为列表框添加项目时,如何确定其唯一性(名称足够)?

Have you tried "listbox.DataSource" with "myItem"-List ? 您是否尝试过将“ listbox.DataSource”与“ myItem” -List一起使用?

Than you can catch the other members of you item... 比起您可以捉住的其他成员...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM