ListView.RetrieveVirtualItem 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生在 ListView 處於虛擬模式,而且需要 ListViewItem 時。
public:
event System::Windows::Forms::RetrieveVirtualItemEventHandler ^ RetrieveVirtualItem;
public event System.Windows.Forms.RetrieveVirtualItemEventHandler RetrieveVirtualItem;
public event System.Windows.Forms.RetrieveVirtualItemEventHandler? RetrieveVirtualItem;
member this.RetrieveVirtualItem : System.Windows.Forms.RetrieveVirtualItemEventHandler
Public Custom Event RetrieveVirtualItem As RetrieveVirtualItemEventHandler
事件類型
例外狀況
在處理 Item 事件時,RetrieveVirtualItem 屬性並未設定為任何項目。
範例
下列程式碼範例示範這個事件的處理常式。 在此範例中,listView1 需要每一個 ListViewItem 顯示其索引的平方。 此程式碼範例是針對 屬性提供的較大範例的 VirtualMode 一部分。
//The basic VirtualMode function. Dynamically returns a ListViewItem
//with the required properties; in this case, the square of the index.
void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
//Caching is not required but improves performance on large sets.
//To leave out caching, don't connect the CacheVirtualItems event
//and make sure myCache is null.
//check to see if the requested item is currently in the cache
if (myCache != null && e.ItemIndex >= firstItem && e.ItemIndex < firstItem + myCache.Length)
{
//A cache hit, so get the ListViewItem from the cache instead of making a new one.
e.Item = myCache[e.ItemIndex - firstItem];
}
else
{
//A cache miss, so create a new ListViewItem and pass it back.
int x = e.ItemIndex * e.ItemIndex;
e.Item = new ListViewItem(x.ToString());
}
}
'The basic VirtualMode function. Dynamically returns a ListViewItem
'with the required properties; in this case, the square of the index.
Private Sub listView1_RetrieveVirtualItem(ByVal sender As Object, ByVal e As RetrieveVirtualItemEventArgs) Handles listView1.RetrieveVirtualItem
'Caching is not required but improves performance on large sets.
'To leave out caching, don't connect the CacheVirtualItems event
'and make sure myCache is null.
'check to see if the requested item is currently in the cache
If Not (myCache Is Nothing) AndAlso e.ItemIndex >= firstItem AndAlso e.ItemIndex < firstItem + myCache.Length Then
'A cache hit, so get the ListViewItem from the cache instead of making a new one.
e.Item = myCache((e.ItemIndex - firstItem))
Else
'A cache miss, so create a new ListViewItem and pass it back.
Dim x As Integer = e.ItemIndex * e.ItemIndex
e.Item = New ListViewItem(x.ToString())
End If
End Sub
備註
ListView當物件處於虛擬模式時,它會動態建立 ListViewItem 物件,而不是使用 Items 集合。 當 物件必須建立 ListViewItem 物件時,就會引發這個事件。 此事件的處理常式應該建立適當的 ListViewItem 或從快取擷取,並透過 Item 屬性傳回它。
如需處理事件的詳細資訊,請參閱 處理和引發事件。