共用方式為


ListView.TopItem 屬性

定義

取得或設定控制項中第一個可見的項目。

public:
 property System::Windows::Forms::ListViewItem ^ TopItem { System::Windows::Forms::ListViewItem ^ get(); };
public:
 property System::Windows::Forms::ListViewItem ^ TopItem { System::Windows::Forms::ListViewItem ^ get(); void set(System::Windows::Forms::ListViewItem ^ value); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListViewItem TopItem { get; }
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListViewItem TopItem { get; set; }
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListViewItem? TopItem { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.TopItem : System.Windows.Forms.ListViewItem
[<System.ComponentModel.Browsable(false)>]
member this.TopItem : System.Windows.Forms.ListViewItem with get, set
Public ReadOnly Property TopItem As ListViewItem
Public Property TopItem As ListViewItem

屬性值

ListViewItem,表示控制項中第一個可見的項目。

屬性

例外狀況

屬性 View 設定為 LargeIconSmallIconTile

範例

下列程式碼範例示範如何使用 TopItem 屬性和 ListViewItem.ListViewSubItem.ResetStyle 方法,重設物件的子專案樣式 ListViewItem 。 若要執行此範例,請將下列程式碼貼到包含名為 Button1 的按鈕的表單中,並在表單的建構函式或 Load 事件處理常式中呼叫 InitializeListView 方法。

internal:
   // Declare the Listview object.
   System::Windows::Forms::ListView^ myListView;

private:

   // Initialize the ListView object with subitems of a different
   // style than the default styles for the ListView.
   void InitializeListView()
   {
      // Set the Location, View and Width properties for the 
      // ListView object. 
      myListView = gcnew ListView;
      myListView->Location = System::Drawing::Point( 20, 20 );
      myListView->Width = 250;
      
      // The View property must be set to Details for the 
      // subitems to be visible.
      myListView->View = View::Details;
      
      // Each SubItem object requires a column, so add three columns.
      this->myListView->Columns->Add( "Key", 50, HorizontalAlignment::Left );
      this->myListView->Columns->Add( "A", 100, HorizontalAlignment::Left );
      this->myListView->Columns->Add( "B", 100, HorizontalAlignment::Left );
      
      // Add a ListItem object to the ListView.
      ListViewItem^ entryListItem = myListView->Items->Add( "Items" );
      
      // Set UseItemStyleForSubItems property to false to change 
      // look of subitems.
      entryListItem->UseItemStyleForSubItems = false;
      
      // Add the expense subitem.
      ListViewItem::ListViewSubItem ^ expenseItem = entryListItem->SubItems->Add( "Expense" );
      
      // Change the expenseItem object's color and font.
      expenseItem->ForeColor = System::Drawing::Color::Red;
      expenseItem->Font = gcnew System::Drawing::Font( "Arial",10,System::Drawing::FontStyle::Italic );
      
      // Add a subitem called revenueItem 
      ListViewItem::ListViewSubItem ^ revenueItem = entryListItem->SubItems->Add( "Revenue" );
      
      // Change the revenueItem object's color and font.
      revenueItem->ForeColor = System::Drawing::Color::Blue;
      revenueItem->Font = gcnew System::Drawing::Font( "Times New Roman",10,System::Drawing::FontStyle::Bold );
      
      // Add the ListView to the form.
      this->Controls->Add( this->myListView );
   }

   void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Use the ListView.TopItem property to access the SubItems
      // and then reset their appearance.
      myListView->TopItem->SubItems[ 1 ]->ResetStyle();
      myListView->TopItem->SubItems[ 2 ]->ResetStyle();
   }

// Declare the Listview object.
internal System.Windows.Forms.ListView myListView;

// Initialize the ListView object with subitems of a different
// style than the default styles for the ListView.
private void InitializeListView()
{

    // Set the Location, View and Width properties for the 
    // ListView object. 
    myListView = new ListView();
    myListView.Location = new System.Drawing.Point(20, 20);
    myListView.Width = 250;

    // The View property must be set to Details for the 
    // subitems to be visible.
    myListView.View = View.Details;
    
    // Each SubItem object requires a column, so add three columns.
    this.myListView.Columns.Add("Key", 50, HorizontalAlignment.Left);
    this.myListView.Columns.Add("A", 100, HorizontalAlignment.Left);
    this.myListView.Columns.Add("B", 100, HorizontalAlignment.Left);

    // Add a ListItem object to the ListView.
    ListViewItem entryListItem = myListView.Items.Add("Items");

    // Set UseItemStyleForSubItems property to false to change 
    // look of subitems.
    entryListItem.UseItemStyleForSubItems = false;

    // Add the expense subitem.
    ListViewItem.ListViewSubItem expenseItem = 
        entryListItem.SubItems.Add("Expense");

    // Change the expenseItem object's color and font.
    expenseItem.ForeColor = System.Drawing.Color.Red;
    expenseItem.Font = new System.Drawing.Font(
        "Arial", 10, System.Drawing.FontStyle.Italic);

    // Add a subitem called revenueItem 
    ListViewItem.ListViewSubItem revenueItem = 
        entryListItem.SubItems.Add("Revenue");

    // Change the revenueItem object's color and font.
    revenueItem.ForeColor = System.Drawing.Color.Blue;
    revenueItem.Font = new System.Drawing.Font(
        "Times New Roman", 10, System.Drawing.FontStyle.Bold);

    // Add the ListView to the form.
    this.Controls.Add(this.myListView);
}

private void Button1_Click(System.Object sender, System.EventArgs e)
{

    // Use the ListView.TopItem property to access the SubItems
    // and then reset their appearance.
    myListView.TopItem.SubItems[1].ResetStyle();
    myListView.TopItem.SubItems[2].ResetStyle();
}

' Declare the Listview object.
Friend WithEvents myListView As System.Windows.Forms.ListView

' Initialize the ListView object with subitems of a different
' style than the default styles for the ListView.
Private Sub InitializeListView()

    ' Set the Location, View and Width properties for the 
    ' ListView object. 
    myListView = New ListView
    With (myListView)
        .Location = New System.Drawing.Point(20, 20)

        ' The View property must be set to Details for the 
        ' subitems to be visible.
        .View = View.Details
        .Width = 250
    End With

    ' Each SubItem object requires a column, so add three columns.
    Me.myListView.Columns.Add("Key", 50, HorizontalAlignment.Left)
    Me.myListView.Columns.Add("A", 100, HorizontalAlignment.Left)
    Me.myListView.Columns.Add("B", 100, HorizontalAlignment.Left)

    ' Add a ListItem object to the ListView.
    Dim entryListItem As ListViewItem = myListView.Items.Add("Items")

    ' Set UseItemStyleForSubItems property to false to change 
    ' look of subitems.
    entryListItem.UseItemStyleForSubItems = False

    ' Add the expense subitem.
    Dim expenseItem As ListViewItem.ListViewSubItem = _
        entryListItem.SubItems.Add("Expense")

    ' Change the expenseItem object's color and font.
    expenseItem.ForeColor = System.Drawing.Color.Red
    expenseItem.Font = New System.Drawing.Font _
        ("Arial", 10, System.Drawing.FontStyle.Italic)

    ' Add a subitem called revenueItem 
    Dim revenueItem As ListViewItem.ListViewSubItem = _
        entryListItem.SubItems.Add("Revenue")

    ' Change the revenueItem object's color and font.
    revenueItem.ForeColor = System.Drawing.Color.Blue
    revenueItem.Font = New System.Drawing.Font _
        ("Times New Roman", 10, System.Drawing.FontStyle.Bold)

    ' Add the ListView to the form.
    Me.Controls.Add(Me.myListView)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' Use the ListView.TopItem property to access the SubItems
    ' and then reset their appearance.
    myListView.TopItem.SubItems(1).ResetStyle()
    myListView.TopItem.SubItems(2).ResetStyle()
End Sub

備註

一開始,索引位置為零的專案 (0) 位於控制項頂 ListView 端。 ListView如果捲動控制項內容,不同的專案可以位於控制項頂端。 您可以使用這個屬性來指出或判斷控制項頂端 ListView 可見的專案。 屬性的值 TopItem 不一定會保存,視所需的頂端專案位於清單檢視中的位置而定。

控制項中 ListView 隨時可見的專案數目取決於清單檢視的高度及其所包含的專案大小。 如果專案超過清單檢視的高度,專案會繼續進入多個頁面,使用者可以捲動。 如果您將 TopItem 屬性設定為 的最後一頁 ListView 中的專案,則專案會自動捲動到檢視中;不過, TopItem 將會設定為最後一頁的實際頂端專案。

若要確保特定專案位於控制項的可見區域中, (但不一定位於頂端位置) ,請使用 EnsureVisible 方法。

注意

當屬性值為 false 時, Scrollable 設定此屬性沒有任何作用。

在 2.0 版之前的 .NET Framework 版本中不支援設定此屬性。

適用於

另請參閱