简体   繁体   English

ListBox:显示多个选定项目?

[英]ListBox: Display multiple selected items?

When I select multiple items in a ListBox , how can I display them? 当我在ListBox选择多个项目时,如何显示它们? Any help will be appreciated. 任何帮助将不胜感激。

First, you need to set the SelectionMode property on your ListBox to either SelectionMode.MultiSimple or SelectionMode.MultiExtended (so that you can select multiple items). 首先,您需要将ListBox上的SelectionMode属性设置为SelectionMode.MultiSimpleSelectionMode.MultiExtended (以便您可以选择多个项目)。

Next, you need to add an event handler for the SelectedIndexChanged event on your ListBox . 接下来,您需要在ListBox上为SelectedIndexChanged事件添加一个事件处理程序。 Within this event handler, accessing the SelectedItems collection of your ListBox will provide you with access to a collection of all the selected objects. 在此事件处理程序中,访问ListBoxSelectedItems集合将使您可以访问所有选定对象的集合。

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 : 这是一个示例事件处理程序,该事件处理程序在名为textBox1TextBox中显示所选项目:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   textBox1.Clear();
   foreach (object selectedItem in listBox1.SelectedItems)
   {
      textBox1.AppendText(selectedItem.ToString() + Environment.NewLine);
   }
}

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

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