ListView.TopItem 属性

获取或设置控件中的第一个可见项。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property TopItem As ListViewItem
用法
Dim instance As ListView
Dim value As ListViewItem

value = instance.TopItem

instance.TopItem = value
public ListViewItem TopItem { get; set; }
public:
property ListViewItem^ TopItem {
    ListViewItem^ get ();
    void set (ListViewItem^ value);
}
/** @property */
public ListViewItem get_TopItem ()

/** @property */
public void set_TopItem (ListViewItem value)
public function get TopItem () : ListViewItem

public function set TopItem (value : ListViewItem)

属性值

ListViewItem,表示控件中的第一个可见项。

异常

异常类型 条件

InvalidOperationException

View 属性设置为 LargeIconSmallIconTile

备注

最初,索引位置零 (0) 处的项位于 ListView 控件的顶端。如果滚动 ListView 控件的内容,就会有不同的项位于控件顶端。可以使用此属性指示或确定哪个项在 ListView 控件的顶部可见。TopItem 属性的值不会始终保持不变,它取决于所需的顶级项在列表视图中的位置。

在任何时候,ListView 控件中的可见项的数目都取决于列表视图的高度和其中包含的项的大小。如果项超出了列表视图的高度,将在多个页中继续显示这些项,用户可以通过滚动页面查看这些项。如果将 TopItem 属性设为 ListView 的最后一页中的某个项,则该项会自动滚动到视图中;但是,TopItem 将被设置为最后一页上实际的顶级项。

若要确保某个特定项位于控件的可见区域中(但不一定是顶级位置),可使用 EnsureVisible 方法。

提示

如果 Scrollable 属性值为 false,设置此属性不会有任何作用。

2.0 版之前的 .NET Framework 版本中不支持设置此属性。

示例

下面的代码示例演示如何通过使用 TopItem 属性和 ListViewItem.ListViewSubItem.ResetStyle 方法重置 ListViewItem 对象的子项的样式。若要运行该示例,请将下面的代码粘贴到包含名为 Button1 的按钮的窗体中,并从该窗体的构造函数或 Load 事件处理程序中调用 InitializeListView 方法。

' 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
// 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();
}
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.
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.set_Location(new System.Drawing.Point(20, 20));
    myListView.set_Width(250);
    // The View property must be set to Details for the 
    // subitems to be visible.
    myListView.set_View(View.Details);
    // Each SubItem object requires a column, so add three columns.
    this.myListView.get_Columns().Add("Key", 50, HorizontalAlignment.Left);
    this.myListView.get_Columns().Add("A", 100, HorizontalAlignment.Left);
    this.myListView.get_Columns().Add("B", 100, HorizontalAlignment.Left);
    // Add a ListItem object to the ListView.
    ListViewItem entryListItem = myListView.get_Items().Add("Items");
    // Set UseItemStyleForSubItems property to false to change 
    // look of subitems.
    entryListItem.set_UseItemStyleForSubItems(false);
    // Add the expense subitem.
    ListViewItem.ListViewSubItem expenseItem =
        entryListItem.get_SubItems().Add("Expense");
    // Change the expenseItem object's color and font.
    expenseItem.set_ForeColor(System.Drawing.Color.get_Red());
    expenseItem.set_Font(new System.Drawing.Font("Arial", 10,
        System.Drawing.FontStyle.Italic));
    // Add a subitem called revenueItem 
    ListViewItem.ListViewSubItem revenueItem =
        entryListItem.get_SubItems().Add("Revenue");
    // Change the revenueItem object's color and font.
    revenueItem.set_ForeColor(System.Drawing.Color.get_Blue());
    revenueItem.set_Font(new System.Drawing.Font("Times New Roman", 10,
        System.Drawing.FontStyle.Bold));
    // Add the ListView to the form.
    this.get_Controls().Add(this.myListView);
} //InitializeListView

private void button1_Click(Object sender, System.EventArgs e)
{
    // Use the ListView.TopItem property to access the SubItems
    // and then reset their appearance.
    myListView.get_TopItem().get_SubItems().get_Item(1).ResetStyle();
    myListView.get_TopItem().get_SubItems().get_Item(2).ResetStyle();
} //button1_Click

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ListView 类
ListView 成员
System.Windows.Forms 命名空间
ListViewItem
EnsureVisible