ListViewItem.ListViewSubItem 類別

定義

表示 ListViewItem 的子項目。

public: ref class ListViewItem::ListViewSubItem
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ListViewSubItemConverter))]
[System.Serializable]
public class ListViewItem.ListViewSubItem
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ListViewSubItemConverter))>]
[<System.Serializable>]
type ListViewItem.ListViewSubItem = class
Public Class ListViewItem.ListViewSubItem
繼承
ListViewItem.ListViewSubItem
屬性

範例

下列程式碼範例會建立控制項,其中指定三 ListViewItem 個物件,以及針對每個專案指定的三 ListViewItem.ListViewSubItemListView 物件。 此範例也會建立 ColumnHeader 物件,以顯示詳細資料檢視中的子專案。 程式碼範例中也會建立兩 ImageList 個 物件,以提供 物件的影像 ListViewItem 。 這些 ImageList 物件會新增至 LargeImageListSmallImageList 屬性。 此範例會在建立 ListView 控制項時使用下列屬性。

您必須將程式碼新增至 , Form 並從表單上的建構函式或其他方法呼叫在範例中建立的方法。 此範例要求名為 MySmallImage1MySmallImage2MyLargeImage1 和 的 MyLargeImage2 映射位於磁片磁碟機 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

備註

ListView控制項會顯示 類別所 ListViewItem 定義的專案清單。 每個 ListViewItem 都可以儲存 類別所 ListViewItem.ListViewSubItem 定義的子專案物件。 當 控制項的 ListView 屬性設定為 DetailsView ,會顯示子專案。 子專案通常包含與其父專案相關的資訊。 例如,如果 ListView 控制項顯示代表訂單的專案,則每個專案都可以顯示訂單編號。 子專案可以新增至每個專案,以顯示資訊,例如已排序的產品、排序的專案數量,以及已排序之專案的總價格。 不同于 ListViewItem 物件, ListViewItem.ListViewSubItem 使用者無法直接編輯物件, (如果控制項的 ListView 屬性設定 true 為) ,使用者就可以編輯 ListViewItem 物件 LabelEdit

由於子專案無法由使用者直接編輯,而且不會顯示影像,因此屬性僅限於在控制項中顯示的 ListView 子專案文字樣式時影響其樣式的屬性。 UseItemStyleForSubItems如果包含子專案的 屬性 ListView 設定 false 為 ,您可以使用 FontBackColorForeColor 屬性來變更套用至文字顯示的樣式。 一般而言,專案和子專案的樣式在控制項中 ListView 都相同,但如果您想要變更特定的 ListViewItem.ListViewSubItem 樣式來反白顯示,您可以在您想要以不同方式顯示的專案上使用這些屬性。

建構函式

ListViewItem.ListViewSubItem()

使用預設值,初始化 ListViewItem.ListViewSubItem 類別的新執行個體。

ListViewItem.ListViewSubItem(ListViewItem, String)

使用指定的擁有人和文字,初始化 ListViewItem.ListViewSubItem 類別的新執行個體。

ListViewItem.ListViewSubItem(ListViewItem, String, Color, Color, Font)

使用指定的擁有人、文字、前景色彩、背景色彩和字型值,初始化 ListViewItem.ListViewSubItem 類別的新執行個體。

屬性

BackColor

取得或設定子項目文字的背景色彩。

Bounds

取得 ListViewItem.ListViewSubItem 的週框 (Bounding Rectangle)。

Font

取得或設定子項目所顯示的文字字型。

ForeColor

取得或設定子項目文字的前景色彩。

Name

取得或設定 ListViewItem.ListViewSubItem 的名稱。

Tag

取得或設定包含 ListViewItem.ListViewSubItem 相關資料的物件。

Text

取得或設定子項目的文字。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ResetStyle()

將套用至子項目的樣式重設為預設的字型和色彩。

ToString()

傳回代表目前物件的字串。

適用於

另請參閱