ListView.MultiSelect 属性

定义

获取或设置一个值,该值指示是否可以选择多个项。

C#
public bool MultiSelect { get; set; }

属性值

如果可以一次选择控件中的多个项,则为 true;否则为 false。 默认值为 true

示例

下面的代码示例演示 ListView 允许选择多个项的 。 该示例演示如何设置 HideSelectionHeaderStyle 属性。 若要运行此示例,请将以下代码粘贴到包含ListView名为 ListView1 的对象和名为 TextBox1TextBox 窗体中。 InitializeListView从窗体的构造函数或 Load 方法调用 方法。

C#
  // This method adds two columns to the ListView, setting the Text 
  // and TextAlign, and Width properties of each ColumnHeader.  The 
  // HeaderStyle property is set to NonClickable since the ColumnClick 
  // event is not handled.  Finally the method adds ListViewItems and 
  // SubItems to each column.
  private void InitializeListView()
  {
      this.ListView1 = new System.Windows.Forms.ListView();
      this.ListView1.BackColor = System.Drawing.SystemColors.Control;
      this.ListView1.Dock = System.Windows.Forms.DockStyle.Top;
      this.ListView1.Location = new System.Drawing.Point(0, 0);
      this.ListView1.Name = "ListView1";
      this.ListView1.Size = new System.Drawing.Size(292, 130);
      this.ListView1.TabIndex = 0;
      this.ListView1.View = System.Windows.Forms.View.Details;
      this.ListView1.MultiSelect = true;
      this.ListView1.HideSelection = false;
      this.ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
      
      ColumnHeader columnHeader1 = new ColumnHeader();
      columnHeader1.Text = "Breakfast Item";
      columnHeader1.TextAlign = HorizontalAlignment.Left;
      columnHeader1.Width = 146;

      ColumnHeader columnHeader2 = new ColumnHeader();
      columnHeader2.Text = "Price Each";
      columnHeader2.TextAlign = HorizontalAlignment.Center;
      columnHeader2.Width = 142;

      this.ListView1.Columns.Add(columnHeader1);
      this.ListView1.Columns.Add(columnHeader2);

      string[] foodList = new string[]{"Juice", "Coffee", 
          "Cereal & Milk", "Fruit Plate", "Toast & Jelly", 
          "Bagel & Cream Cheese"};
      string[] foodPrice = new string[]{"1.09", "1.09", "2.19", 
          "2.49", "1.49", "1.49"};
      
      for(int count=0; count < foodList.Length; count++)
      {
          ListViewItem listItem = new ListViewItem(foodList[count]);
          listItem.SubItems.Add(foodPrice[count]);
          ListView1.Items.Add(listItem);
      }
      this.Controls.Add(ListView1);
  }

注解

当 属性 MultiSelect 设置为 true时,可以在 控件中选择 ListView 多个项。 若要选择多个项,用户必须按住 Ctrl 键,同时单击要选择的项目。 可以通过单击要选择的第一个项目来选择连续项,然后在按住 SHIFT 键的同时单击要选择的最后一项。 可以使用多重选择功能在控件中选择多个项, ListView 并对所有所选项执行操作。 例如,用户可以选择多个项,然后右键单击所选项以显示快捷菜单,该菜单显示一组可对所选项目执行的任务。

若要确定在 控件中选择 ListView 了哪些项,请使用 SelectedItems 属性。 属性 SelectedItems 允许访问 ListView.SelectedListViewItemCollection 包含所选项列表的 。 如果希望索引位置位于 而不是 ListView.ListViewItemCollection 项中,则可以使用 SelectedIndices 属性访问 ListView.SelectedIndexCollection

如果不想在 中 ListView允许多个选择,但仍希望为用户提供选择多个项的方法,可以通过将 属性设置为 CheckBoxestrue来显示复选框。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅