ListView.CacheVirtualItems Event

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.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public event CacheVirtualItemsEventHandler CacheVirtualItems

This event only occurs when VirtualMode is true. 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 Consuming Events.

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. 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());
    }

}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft