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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:04:40  来源:igfitidea点击:

ListBox: Display multiple selected items?

c#.netwindowslistboxselecteditem

提问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 SelectionModeproperty on your ListBoxto either SelectionMode.MultiSimpleor SelectionMode.MultiExtended(so that you canselect multiple items).

首先,您需要将您的SelectionMode属性设置ListBoxSelectionMode.MultiSimpleSelectionMode.MultiExtended(以便您可以选择多个项目)。

Next, you need to add an event handler for the SelectedIndexChangedevent on your ListBox. Within this event handler, accessing the SelectedItemscollection of your ListBoxwill 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 TextBoxcalled 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);
   }
}