ListView.TopItem Property

Definition

获取或设置控件中的第一个可见项。Gets or sets the first visible item in the control.

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

Property Value

ListViewItem

ListViewItem,表示控件中的第一个可见项。A ListViewItem that represents the first visible item in the control.

Attributes

Exceptions

View 属性设置为 LargeIconSmallIconTileThe View property is set to LargeIcon, SmallIcon, or Tile.

Examples

下面的代码示例演示如何通过使用 TopItem 属性和 ListViewItem.ListViewSubItem.ResetStyle 方法来重置 ListViewItem 对象的子项的样式。The following code example demonstrates resetting the style of a ListViewItem object's subitems through the use of the TopItem property and the ListViewItem.ListViewSubItem.ResetStyle method. 若要运行该示例,请将以下代码粘贴到包含名为 Button1 的按钮的窗体中,然后在窗体的构造函数或 Load 事件处理程序中调用 InitializeListView 方法。To run the example, paste the following code into a form containing a button named Button1 and call the InitializeListView method in the form's constructor or Load event handler.

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

Remarks

最初,索引位置为零(0)的项位于 ListView 控件的顶部。Initially, the item with the index position of zero (0) is at the top of the ListView control. 如果滚动 ListView 控件内容,则在控件的顶部可以有不同的项。If the ListView control contents are scrolled, a different item can be at the top of the control. 您可以使用此属性来指示或确定哪个项在 ListView 控件的顶部可见。You can use this property to indicate or determine which item is visible at the top of the ListView control. TopItem 属性的值不会始终保留,这取决于所需的顶级项在列表视图中的位置。The value of the TopItem property will not always persist, depending on where the desired top item is in the list view.

ListView 控件中随时可见的项的数目取决于列表视图的高度以及它所包含的项的大小。The number of items visible in a ListView control at any time depends on the height of the list view and the size of the items it contains. 如果项超过列表视图的高度,则项将继续在多个页上,用户可以滚动浏览。If the items exceed the height of the list view, the items will continue onto multiple pages, which the user can scroll through. 如果将 TopItem 属性设置为 ListView的最后一页中的项,则该项将自动滚动到视图中;但 TopItem 将设置为最后一页的实际顶部项。If you set the TopItem property to an item in the last page of the ListView, the item will automatically be scrolled into view; however, TopItem will be set to the actual top item of the last page.

若要确保特定项位于控件的可见区域(但不一定在顶部位置),请使用 EnsureVisible 方法。To ensure that a specific item is in the visible region of the control (but not necessarily in the top position), use the EnsureVisible method.

Note

falseScrollable 属性值时,设置此属性不起作用。Setting this property has no effect when the Scrollable property value is false.

在版本2.0 之前的 .NET Framework.NET Framework 版本中不支持设置此属性。Setting this property is not supported in versions of the .NET Framework.NET Framework prior to version 2.0.

Applies to

See also