如何在 C# 和 WinForms 中向 ListBox 添加项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1732054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add an item to a ListBox in C# and WinForms?
提问by Ante
I'm having trouble figuring out how to add items to a ListBox
in WinForms.
我在弄清楚如何向ListBox
WinForms 中的a 添加项目时遇到了问题。
I have tried:
我试过了:
list.DisplayMember = "clan";
list.ValueMember = sifOsoba;
How can I add ValueMember
to the list with an int
value and some text for the DisplayMember
?
如何将值和一些文本添加ValueMember
到列表int
中DisplayMember
?
list.Items.add(?)
Btw. I can't use ListBoxItem
for any reasons.
顺便提一句。我无法使用ListBoxItem
任何原因。
采纳答案by monksy
list.Items.add(new ListBoxItem("name", "value"));
The internal (default) data structure of the ListBox is the ListBoxItem.
ListBox 的内部(默认)数据结构是ListBoxItem。
回答by user210748
You have to create an item of type ListBoxItem and add that to the Items collection:
您必须创建一个 ListBoxItem 类型的项目并将其添加到 Items 集合中:
list.Items.add( new ListBoxItem("clan", "sifOsoba"));
回答by John Rudy
In WinForms, ValueMember
and DisplayMember
are used when data-binding the list. If you're not data-binding, then you can add any arbitrary object as a ListItem
.
在 WinForms 中,ValueMember
并DisplayMember
在数据绑定列表时使用。如果您不是数据绑定,那么您可以将任意对象添加为ListItem
.
The catch to that is that, in order to display the item, ToString()
will be called on it. Thus, it is highly recommended that you only add objects to the ListBox where calling ToString()
will result in meaningful output.
问题在于,为了显示该项目,ToString()
将对其进行调用。因此,强烈建议您仅将对象添加到调用ToString()
将产生有意义的输出的 ListBox 。
回答by zincorp
DisplayMember and ValueMember are mostly only useful if you're databinding to objects that have those properties defined. You would then need to add an instance of that object.
DisplayMember 和 ValueMember 通常仅在您将数据绑定到定义了这些属性的对象时才有用。然后,您需要添加该对象的实例。
e.g.:
例如:
public class MyObject
{
public string clan { get; set; }
public int sifOsoba { get; set; }
public MyObject(string aClan, int aSif0soba)
{
this.clan = aClan;
this.sif0soba = aSif0soba;
}
public override string ToString() { return this.clan; }
}
....
list.Items.Add(new MyObject("hello", 5));
If you're binding it manually then you can use the example provided by goggles
如果您手动绑定它,那么您可以使用 goggles 提供的示例
回答by Kelsey
You might want to checkout this SO question:
您可能想查看这个 SO 问题:
C# - WinForms - What is the proper way to load up a ListBox?
回答by Philip Wallace
If you are adding integers, as you say in your question, this will add 50 (from 1 to 50):
如果您要添加整数,如您在问题中所说,这将添加 50(从 1 到 50):
for (int x = 1; x <= 50; x++)
{
list.Items.Add(x);
}
You do not need to set DisplayMember and ValueMember unless you are adding objects that have specific properties that you want to display to the user. In your example:
您不需要设置 DisplayMember 和 ValueMember,除非您要添加具有要向用户显示的特定属性的对象。在你的例子中:
listbox1.Items.Add(new { clan = "Foo", sifOsoba = 1234 });
回答by PostMan
The way I do this - using the format Event
我这样做的方式 - 使用格式 Event
MyClass c = new MyClass();
listBox1.Items.Add(c);
private void listBox1_Format(object sender, ListControlConvertEventArgs e)
{
if(e.ListItem is MyClass)
{
e.Value = ((MyClass)e.ListItem).ToString();
}
else
{
e.Value = "Unknown item added";
}
}
e.Value being the Display Text
e.Value 是显示文本
Then you can attempt to cast the SelectedItem to MyClass to get access to anything you had in there.
Also note, you can use anything (that inherits from object anyway(which is pretty much everything)) in the Items Collection.
然后您可以尝试将 SelectedItem 转换为 MyClass 以访问您在其中拥有的任何内容。
另请注意,您可以在 Items Collection 中使用任何内容(无论如何都继承自对象(几乎是所有内容))。
回答by AZ.
ListBoxItem is a WPF class, NOT a WinForms class.
ListBoxItem 是 WPF 类,而不是 WinForms 类。
For WPF, use ListBoxItem.
对于 WPF,请使用 ListBoxItem。
For WinForms, the item is a Object type, so use one of these:
1. Provide your own ToString() method for the Object type.
2. Use databinding with DisplayMemeber and ValueMember (see Kelsey's answer)
对于 WinForms,该项目是 Object 类型,因此请使用以下其中一种:
1. 为 Object 类型提供您自己的 ToString() 方法。
2. 将数据绑定与 DisplayMemeber 和 ValueMember 一起使用(请参阅 Kelsey 的回答)
回答by netfed
If you just want to add a string to it, the simple answer is:
如果你只想给它添加一个字符串,简单的答案是:
ListBox.Items.Add("some text");