ListView.TopItem 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置控件中的第一个可见项。
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,表示控件中的第一个可见项。
- 属性
例外
示例
下面的代码示例演示如何通过使用 属性和 ListViewItem.ListViewSubItem.ResetStyle 方法重置对象的子项的TopItem样式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 的项 (0) 位于控件的 ListView 顶部。 ListView如果滚动控件内容,则控件顶部可以有其他项。 可以使用此属性来指示或确定在控件顶部可见的 ListView 项。 属性的值 TopItem 不会始终保留,具体取决于所需顶部项在列表视图中的位置。
控件中 ListView 随时可见的项数取决于列表视图的高度及其包含的项的大小。 如果项超过列表视图的高度,则这些项将继续到多个页面上,用户可以滚动浏览这些页面。 如果将 属性设置为 TopItem 的最后一页 ListView中的某个项,则会自动滚动到视图中;但是, TopItem 将设置为最后一页的实际顶部项。
若要确保特定项位于控件 (的可见区域,但不一定位于) 的顶部位置,请使用 EnsureVisible 方法。