ListView.MultiSelect Property

Definition

获取或设置一个值,该值指示是否可以选择多个项。Gets or sets a value indicating whether multiple items can be selected.

public:
 property bool MultiSelect { bool get(); void set(bool value); };
public bool MultiSelect { get; set; }
member this.MultiSelect : bool with get, set
Public Property MultiSelect As Boolean

Property Value

Boolean

如果可以一次选择控件中的多个项,则为 true;否则为 falsetrue if multiple items in the control can be selected at one time; otherwise, false. 默认为 trueThe default is true.

Examples

下面的代码示例演示一个允许选择多个项的 ListViewThe following code example demonstrates a ListView that allows multiple items to be selected. 该示例演示如何设置 HideSelectionHeaderStyle 属性。The example demonstrates setting the HideSelection and HeaderStyle properties. 若要运行此示例,请将以下代码粘贴到包含名为 ListView1ListView 对象和一个名为 TextBox1TextBox 的窗体中。To run this example, paste the following code into a form that contains a ListView object named ListView1 and a TextBox named TextBox1. 从窗体的构造函数或 Load 方法中调用 InitializeListView 方法。Call the InitializeListView method from the form's constructor or Load method.

// 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.
void InitializeListView()
{
   this->ListView1 = gcnew System::Windows::Forms::ListView;
   this->ListView1->BackColor = System::Drawing::SystemColors::Control;
   this->ListView1->Dock = System::Windows::Forms::DockStyle::Top;
   this->ListView1->Location = System::Drawing::Point( 0, 0 );
   this->ListView1->Name = "ListView1";
   this->ListView1->Size = 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 = gcnew ColumnHeader;
   columnHeader1->Text = "Breakfast Item";
   columnHeader1->TextAlign = HorizontalAlignment::Left;
   columnHeader1->Width = 146;
   ColumnHeader^ columnHeader2 = gcnew ColumnHeader;
   columnHeader2->Text = "Price Each";
   columnHeader2->TextAlign = HorizontalAlignment::Center;
   columnHeader2->Width = 142;
   this->ListView1->Columns->Add( columnHeader1 );
   this->ListView1->Columns->Add( columnHeader2 );
   array<String^>^foodList = {"Juice","Coffee","Cereal & Milk","Fruit Plate","Toast & Jelly","Bagel & Cream Cheese"};
   array<String^>^foodPrice = {"1.09","1.09","2.19","2.49","1.49","1.49"};
   for ( int count = 0; count < foodList->Length; count++ )
   {
      ListViewItem^ listItem = gcnew ListViewItem( foodList[ count ] );
      listItem->SubItems->Add( foodPrice[ count ] );
      ListView1->Items->Add( listItem );

   }
   this->Controls->Add( ListView1 );
}
  // 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);
  }
  
' 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 Sub InitializeListView()
    Me.ListView1 = New System.Windows.Forms.ListView
    Me.ListView1.BackColor = System.Drawing.SystemColors.Control
    Me.ListView1.Dock = System.Windows.Forms.DockStyle.Top
    Me.ListView1.Location = New System.Drawing.Point(0, 0)
    Me.ListView1.Name = "ListView1"
    Me.ListView1.Size = New System.Drawing.Size(292, 130)
    Me.ListView1.TabIndex = 0
    Me.ListView1.View = System.Windows.Forms.View.Details
    Me.ListView1.MultiSelect = True
    Me.ListView1.HideSelection = False
    ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable
    Dim columnHeader1 As New ColumnHeader
    With columnHeader1
        .Text = "Breakfast Item"
        .TextAlign = HorizontalAlignment.Left
        .Width = 146
    End With
    Dim columnHeader2 As New ColumnHeader
    With columnHeader2
        .Text = "Price Each"
        .TextAlign = HorizontalAlignment.Center
        .Width = 142
    End With

    Me.ListView1.Columns.Add(columnHeader1)
    Me.ListView1.Columns.Add(columnHeader2)
    Dim foodList() As String = New String() {"Juice", "Coffee", _
        "Cereal & Milk", "Fruit Plate", "Toast & Jelly", _
        "Bagel & Cream Cheese"}
    Dim foodPrice() As String = New String() {"1.09", "1.09", _
        "2.19", "2.49", "1.49", "1.49"}
    Dim count As Integer
    For count = 0 To foodList.Length - 1
        Dim listItem As New ListViewItem(foodList(count))
        listItem.SubItems.Add(foodPrice(count))
        ListView1.Items.Add(listItem)
    Next
    Me.Controls.Add(Me.ListView1)
End Sub

Remarks

MultiSelect 属性设置为 true时,可在 ListView 控件中选择多个项。When the MultiSelect property is set to true, multiple items can be selected in the ListView control. 若要选择多个项,用户必须在单击要选择的项的同时按住 CTRL 键。To select multiple items, the user must hold down the CTRL key while clicking the items to select. 通过单击要选择的第一项,然后在按住 SHIFT 键的同时单击最后一个要选择的项,可以选择连续项。Consecutive items can be selected by clicking the first item to select and then, while holding down the SHIFT key, clicking the last item to select. 您可以使用 "多重选择" 功能选择 ListView 控件中的多个项,并对所有选定项执行操作。You can use the multiple selection feature to select multiple items in the ListView control and perform an operation on all the items selected. 例如,用户可以选择多个项目,然后右键单击所选项目以显示一个快捷菜单,其中显示了一组可对所选项目执行的任务。For example, the user could select multiple items and then right-click a selected item to display a shortcut menu that displays a set of tasks that can be performed on the selected items.

若要确定 ListView 控件中选择了哪些项,请使用 "SelectedItems" 属性。To determine which items are selected in the ListView control, use the SelectedItems property. SelectedItems 属性允许您访问包含选定项的列表的 ListView.SelectedListViewItemCollectionThe SelectedItems property allows you to access the ListView.SelectedListViewItemCollection that contains a list of the selected items. 如果希望 ListView.ListViewItemCollection 中的索引位置而不是项,则可以使用 SelectedIndices 属性来访问 ListView.SelectedIndexCollectionIf you want the index positions in the ListView.ListViewItemCollection instead of the items, you can use the SelectedIndices property to access the ListView.SelectedIndexCollection.

如果你不希望允许在 ListView中进行多项选择,但仍希望为用户提供一种方法来选择多个项目,则可以通过将 CheckBoxes 属性设置为 true来显示复选框。If you do not want to allow multiple selections in the ListView, yet still want to offer the user a way for multiple items to be chosen, you can display check boxes by setting the CheckBoxes property to true.

Applies to

See also