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

屬性值

Boolean

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

範例

下列程式碼範例會建立一個 ListView 具有指定三個物件的控制項 ListViewItem ,以及 ListViewItem.ListViewSubItem 為每個專案指定的三個物件。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. 在程式 ImageList 代碼範例中也會建立兩個物件,以提供 ListViewItem 物件的影像。Two ImageList objects are also created in the code example to provide images for the ListViewItem objects. 這些 ImageList 物件會加入至 LargeImageListSmallImageList 屬性。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. 此範例也需要名稱為、、和的映射 MySmallImage1 MySmallImage2 MyLargeImage1 MyLargeImage2 位於磁片磁碟機 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

備註

CheckBoxes屬性可讓您在清單中的每個專案旁邊顯示覆選框。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. CheckBoxes屬性提供一種方式,可在 ListView 不使用 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. 即使控制項的 MultiSelect 屬性 ListView 設定為 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.

若要判斷專案的檢查時間,請建立事件的事件處理常式 ItemCheckTo determine when an item has been checked, create an event handler for the ItemCheck event. 若要取得中已簽入的所有專案 ListView ,請使用 CheckedItems 屬性來存取 ListView.CheckedIndexCollection 控制項的。To 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.

如果 ImageList 在屬性中指定 StateImageList ,則會顯示在索引位置0和1的影像, ImageList 而不是顯示覆選框。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.

注意

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

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

適用於

另請參閱