windows ListBox:显示多个选定的项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1362335/
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
ListBox: Display multiple selected items?
提问by Surya sasidhar
When I select multiple items in a ListBox
, how can I display them? Any help will be appreciated.
当我在 a 中选择多个项目时ListBox
,如何显示它们?任何帮助将不胜感激。
回答by Donut
First, you need to set the SelectionMode
property on your ListBox
to either SelectionMode.MultiSimple
or SelectionMode.MultiExtended
(so that you canselect multiple items).
首先,您需要将您的SelectionMode
属性设置ListBox
为SelectionMode.MultiSimple
或SelectionMode.MultiExtended
(以便您可以选择多个项目)。
Next, you need to add an event handler for the SelectedIndexChanged
event on your ListBox
. Within this event handler, accessing the SelectedItems
collection of your ListBox
will provide you with access to a collection of all the selected objects.
接下来,您需要为SelectedIndexChanged
您的ListBox
. 在此事件处理程序中,访问SelectedItems
您的集合ListBox
将为您提供对所有选定对象的集合的访问。
From there, you can iterate through the collection to display the objects in any manner you choose. Here's an example event handler that displays the selected items in a TextBox
called textBox1
:
从那里,您可以遍历集合,以您选择的任何方式显示对象。这是一个示例事件处理程序,它在一个TextBox
被调用的中显示所选项目textBox1
:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Clear();
foreach (object selectedItem in listBox1.SelectedItems)
{
textBox1.AppendText(selectedItem.ToString() + Environment.NewLine);
}
}