ListView.CacheVirtualItems ListView.CacheVirtualItems ListView.CacheVirtualItems ListView.CacheVirtualItems Event

定义

发生时的显示区域的内容ListView处于虚拟模式已更改,并ListView决定需要的项的新范围。Occurs when the contents of the display area for a ListView in virtual mode has changed, and the ListView determines that a new range of items is needed.

public:
 event System::Windows::Forms::CacheVirtualItemsEventHandler ^ CacheVirtualItems;
public event System.Windows.Forms.CacheVirtualItemsEventHandler CacheVirtualItems;
member this.CacheVirtualItems : System.Windows.Forms.CacheVirtualItemsEventHandler 
Public Custom Event CacheVirtualItems As CacheVirtualItemsEventHandler 

示例

下面的代码示例演示如何将此成员。The following code example demonstrates the use of this member. 在示例中,事件处理程序会检查以确保缓存刷新时才真正需要,然后重新生成缓存。In the example, the event handler checks to make sure a cache refresh is really necessary, and then rebuilds the cache. 此代码示例是为提供一个更大示例的一部分VirtualMode属性。This code example is part of a larger example provided for the VirtualMode property.

//Manages the cache.  ListView calls this when it might need a 
//cache refresh.
void listView1_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
{
    //We've gotten a request to refresh the cache.
    //First check if it's really neccesary.
    if (myCache != null && e.StartIndex >= firstItem && e.EndIndex <= firstItem + myCache.Length)
    {
        //If the newly requested cache is a subset of the old cache, 
        //no need to rebuild everything, so do nothing.
        return;
    }

    //Now we need to rebuild the cache.
    firstItem = e.StartIndex;
    int length = e.EndIndex - e.StartIndex + 1; //indexes are inclusive
    myCache = new ListViewItem[length];
    
    //Fill the cache with the appropriate ListViewItems.
    int x = 0;
    for (int i = 0; i < length; i++)
    {
        x = (i + firstItem) * (i + firstItem);
        myCache[i] = new ListViewItem(x.ToString());
    }

}
'Manages the cache.  ListView calls this when it might need a 
'cache refresh.
Private Sub listView1_CacheVirtualItems(ByVal sender As Object, ByVal e As CacheVirtualItemsEventArgs) Handles listView1.CacheVirtualItems
    'We've gotten a request to refresh the cache.
    'First check if it's really neccesary.
    If Not (myCache Is Nothing) AndAlso e.StartIndex >= firstItem AndAlso e.EndIndex <= firstItem + myCache.Length Then
        'If the newly requested cache is a subset of the old cache, 
        'no need to rebuild everything, so do nothing.
        Return
    End If

    'Now we need to rebuild the cache.
    firstItem = e.StartIndex
    Dim length As Integer = e.EndIndex - e.StartIndex + 1 'indexes are inclusive
    myCache = New ListViewItem(length) {}

    'Fill the cache with the appropriate ListViewItems.
    Dim x As Integer = 0
    Dim i As Integer
    For i = 0 To length
        x = (i + firstItem) * (i + firstItem)
        myCache(i) = New ListViewItem(x.ToString())
    Next i

End Sub

注解

才会发生此事件时VirtualModetrueThis event only occurs when VirtualMode is true. 处理此事件允许ListView可更新项信息保存在缓存中,以便能轻松获得。Handling this event allows the ListView to update the item information held in the cache so that it is readily available. 这可以提高大列表或列表的项是计算成本很高的性能。This can improve performance on large lists, or lists whose items are expensive to calculate.

有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

适用于