ListView.CheckBoxes 屬性

定義

取得或設定值,指出控制項中每個項目的旁邊是否要出現核取方塊。Gets or sets a value indicating whether a check box appears next to each item in the control.

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

屬性值

如果 ListView 控制項中每個項目的旁邊都要出現核取方塊,則為 true,否則為 falsetrue if a check box appears next to each item in the ListView control; otherwise, false. 預設為 falseThe default is false.

範例

下列程式碼範例會建立一個 @no__t 0 控制項,其中指定了三個 ListViewItem 物件,以及為每個專案指定三個 @no__t 2 個物件。The following code example creates a ListView control with three ListViewItem objects specified and three ListViewItem.ListViewSubItem objects specified for each item. 此範例也會建立 ColumnHeader 物件,以在詳細資料檢視中顯示子元件。The example also creates ColumnHeader objects to display the subitems in details view. 程式碼範例中也會建立兩個 @no__t 0 的物件,以提供 @no__t 1 物件的影像。Two ImageList objects are also created in the code example to provide images for the ListViewItem objects. 這些 ImageList 物件會加入至 LargeImageList 和 @no__t 2 屬性。These ImageList objects are added to the LargeImageList and SmallImageList properties. 此範例會使用下列屬性來建立 ListView 控制項。The example uses the following properties in creating the ListView control.

這個範例需要您已將程式碼新增至 Form,並從函式或表單上的另一個方法呼叫在範例中建立的方法。This example requires that you have added the code to a Form and call the method created in the example from the constructor or another method on the form. 此範例也需要名為 MySmallImage1MySmallImage2MyLargeImage1MyLargeImage2 的映射位於 C 磁片磁碟機的根目錄中。The example also requires that images named MySmallImage1, MySmallImage2, MyLargeImage1, and MyLargeImage2 are located in the root directory of drive C.

private:
   void CreateMyListView()
   {
      // Create a new ListView control.
      ListView^ listView1 = gcnew ListView;
      listView1->Bounds = Rectangle(Point(10,10),System::Drawing::Size( 300, 200 ));

      // Set the view to show details.
      listView1->View = View::Details;

      // Allow the user to edit item text.
      listView1->LabelEdit = true;

      // Allow the user to rearrange columns.
      listView1->AllowColumnReorder = true;

      // Display check boxes.
      listView1->CheckBoxes = true;

      // Select the item and subitems when selection is made.
      listView1->FullRowSelect = true;

      // Display grid lines.
      listView1->GridLines = true;

      // Sort the items in the list in ascending order.
      listView1->Sorting = SortOrder::Ascending;

      // Create three items and three sets of subitems for each item.
      ListViewItem^ item1 = gcnew ListViewItem( "item1",0 );

      // Place a check mark next to the item.
      item1->Checked = true;
      item1->SubItems->Add( "1" );
      item1->SubItems->Add( "2" );
      item1->SubItems->Add( "3" );
      ListViewItem^ item2 = gcnew ListViewItem( "item2",1 );
      item2->SubItems->Add( "4" );
      item2->SubItems->Add( "5" );
      item2->SubItems->Add( "6" );
      ListViewItem^ item3 = gcnew ListViewItem( "item3",0 );

      // Place a check mark next to the item.
      item3->Checked = true;
      item3->SubItems->Add( "7" );
      item3->SubItems->Add( "8" );
      item3->SubItems->Add( "9" );

      // Create columns for the items and subitems.
      // Width of -2 indicates auto-size.
      listView1->Columns->Add( "Item Column", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 2", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 3", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 4", -2, HorizontalAlignment::Center );

      //Add the items to the ListView.
      array<ListViewItem^>^temp1 = {item1,item2,item3};
      listView1->Items->AddRange( temp1 );

      // Create two ImageList objects.
      ImageList^ imageListSmall = gcnew ImageList;
      ImageList^ imageListLarge = gcnew ImageList;

      // Initialize the ImageList objects with bitmaps.
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage1.bmp" ) );
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage2.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage1.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage2.bmp" ) );

      //Assign the ImageList objects to the ListView.
      listView1->LargeImageList = imageListLarge;
      listView1->SmallImageList = imageListSmall;
      
      // Add the ListView to the control collection.
      this->Controls->Add( listView1 );
   }
private void CreateMyListView()
{
    // Create a new ListView control.
    ListView listView1 = new ListView();
    listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

    // Set the view to show details.
    listView1.View = View.Details;
    // Allow the user to edit item text.
    listView1.LabelEdit = true;
    // Allow the user to rearrange columns.
    listView1.AllowColumnReorder = true;
    // Display check boxes.
    listView1.CheckBoxes = true;
    // Select the item and subitems when selection is made.
    listView1.FullRowSelect = true;
    // Display grid lines.
    listView1.GridLines = true;
    // Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending;
                
    // Create three items and three sets of subitems for each item.
    ListViewItem item1 = new ListViewItem("item1",0);
    // Place a check mark next to the item.
    item1.Checked = true;
    item1.SubItems.Add("1");
    item1.SubItems.Add("2");
    item1.SubItems.Add("3");
    ListViewItem item2 = new ListViewItem("item2",1);
    item2.SubItems.Add("4");
    item2.SubItems.Add("5");
    item2.SubItems.Add("6");
    ListViewItem item3 = new ListViewItem("item3",0);
    // Place a check mark next to the item.
    item3.Checked = true;
    item3.SubItems.Add("7");
    item3.SubItems.Add("8");
    item3.SubItems.Add("9");

    // Create columns for the items and subitems.
    // Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

    //Add the items to the ListView.
    listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

    // Create two ImageList objects.
    ImageList imageListSmall = new ImageList();
    ImageList imageListLarge = new ImageList();

    // Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

    //Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge;
    listView1.SmallImageList = imageListSmall;

    // Add the ListView to the control collection.
    this.Controls.Add(listView1);
}
Private Sub CreateMyListView()
    ' Create a new ListView control.
    Dim listView1 As New ListView()
    listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200))

    ' Set the view to show details.
    listView1.View = View.Details
    ' Allow the user to edit item text.
    listView1.LabelEdit = True
    ' Allow the user to rearrange columns.
    listView1.AllowColumnReorder = True
    ' Display check boxes.
    listView1.CheckBoxes = True
    ' Select the item and subitems when selection is made.
    listView1.FullRowSelect = True
    ' Display grid lines.
    listView1.GridLines = True
    ' Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending

    ' Create three items and three sets of subitems for each item.
    Dim item1 As New ListViewItem("item1", 0)
    ' Place a check mark next to the item.
    item1.Checked = True
    item1.SubItems.Add("1")
    item1.SubItems.Add("2")
    item1.SubItems.Add("3")
    Dim item2 As New ListViewItem("item2", 1)
    item2.SubItems.Add("4")
    item2.SubItems.Add("5")
    item2.SubItems.Add("6")
    Dim item3 As New ListViewItem("item3", 0)
    ' Place a check mark next to the item.
    item3.Checked = True
    item3.SubItems.Add("7")
    item3.SubItems.Add("8")
    item3.SubItems.Add("9")

    ' Create columns for the items and subitems.
    ' Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)

    'Add the items to the ListView.
    listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})

    ' Create two ImageList objects.
    Dim imageListSmall As New ImageList()
    Dim imageListLarge As New ImageList()

    ' Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage1.bmp"))
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage2.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage1.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage2.bmp"))

    'Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge
    listView1.SmallImageList = imageListSmall

    ' Add the ListView to the control collection.
    Me.Controls.Add(listView1)
End Sub

備註

[@No__t-0] 屬性可讓您在清單中的每個專案旁顯示覆選框。The CheckBoxes property allows you to display a check box next to each item in the list. 這可讓您的應用程式顯示專案清單(以及如果 View 屬性設定為 View.Details),使用者可以按一下核取方塊來選取此選項。This enables your application to display a list of items (and subitems if the View property is set to View.Details) that the user can select by clicking the check box. [@No__t-0] 屬性提供一種方法,可讓您在 @no__t 1 控制項中選取多個專案,而不需要使用 CTRL 鍵。The CheckBoxes property offers a way to select multiple items in the ListView control without using the CTRL key. 根據您的應用程式而定,使用核取方塊來選取專案,而不是標準的多重選取方法,對於使用者而言可能更容易。Depending on your application, using check boxes to select items rather than the standard multiple selection method may be easier for the user. 即使 ListView 控制項的 MultiSelect 屬性設定為 false,您仍然可以顯示覆選框,並為使用者提供多個選取功能。Even if the MultiSelect property of the ListView control is set to false, you can still display checkboxes and provide multiple selection capabilities to the user. 如果您不想要選取多個專案,但仍想讓使用者從清單中選擇多個專案來執行應用程式內的作業,這項功能會很有用。This feature can be useful if you do not want multiple items to be selected yet still want to allow the user to choose multiple items from the list to perform an operation within your application.

若要判斷專案何時已核取,請建立 ItemCheck 事件的事件處理常式。To determine when an item has been checked, create an event handler for the ItemCheck event. 若要取得簽入 ListView 的所有專案,請使用 CheckedItems 屬性來存取控制項的 ListView.CheckedIndexCollectionTo get all the items that are checked in the ListView, use the CheckedItems property to access the ListView.CheckedIndexCollection for the control. 若要取得 ListView 中簽入之所有專案的索引,請使用 CheckedIndices 屬性。To get the indexes of all items that are checked in the ListView, use the CheckedIndices property.

如果 StateImageList 屬性中指定了 ImageList,則會顯示在 ImageList 中索引位置0和1的影像,而非核取方塊。If an ImageList is specified in the StateImageList property, the images at index positions 0 and 1 in the ImageList are displayed instead of the check box. 會顯示位於索引位置0的影像,而不是未核取的核取方塊,而會顯示位於索引位置1的影像,而不是已核取的核取方塊。The image at index position 0 is displayed instead of the unchecked check box, and the image at index position 1 is displayed instead of the checked check box.

注意

當您將此屬性設定為 true 時,就會發生 ItemCheck 事件。When you set this property to true, the ItemCheck event occurs.

當 [View] 屬性設定為 [Tile] 時,不會顯示覆選框。Check boxes are not displayed when the View property is set to Tile.

適用於

另請參閱