ListView.VirtualMode ListView.VirtualMode ListView.VirtualMode ListView.VirtualMode Property

정의

사용자가 직접 ListView 컨트롤에 대한 데이터 관리 작업을 제공했는지 여부를 나타내는 값을 가져오거나 설정합니다.Gets or sets a value indicating whether you have provided your own data-management operations for the ListView control.

public:
 property bool VirtualMode { bool get(); void set(bool value); };
public bool VirtualMode { get; set; }
member this.VirtualMode : bool with get, set
Public Property VirtualMode As Boolean

속성 값

true 하는 경우 ListView 가 제공 하는 데이터 관리 작업을 사용 하 여이 고, 그렇지 false합니다.true if ListView uses data-management operations that you provide; otherwise, false. 기본값은 false입니다.The default is false.

예외

VirtualMode 로 설정 된 true 다음 조건 중 하나가 존재 합니다.VirtualMode is set to true and one of the following conditions exist:

  • VirtualListSize is greater than 0 and RetrieveVirtualItem is not handled.
  • 또는-or-

  • Items, CheckedItems, or SelectedItems contains items.
  • 또는-or- - Items를 편집했습니다.- Edits are made to Items.

    예제

    이 예제는 간단한 ListView 내용이 먼저 10 천 제곱 합니다.This example illustrates a simple ListView whose contents are the first ten thousand squares. 검색을 처리 하 고 성능 향상된을 위해 캐시를 사용 합니다.It handles searching and uses a cache for increased performance.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    
    public class Form1 : Form
    {
        private ListViewItem[] myCache; //array to cache items for the virtual list
        private int firstItem; //stores the index of the first item in the cache
    
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    
        public Form1()
        {
            //Create a simple ListView.
            ListView listView1 = new ListView();
            listView1.View = View.SmallIcon;
            listView1.VirtualMode = true;
            listView1.VirtualListSize = 10000;
    
            //Hook up handlers for VirtualMode events.
            listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView1_RetrieveVirtualItem);
            listView1.CacheVirtualItems += new CacheVirtualItemsEventHandler(listView1_CacheVirtualItems);
            listView1.SearchForVirtualItem += new SearchForVirtualItemEventHandler(listView1_SearchForVirtualItem);
    
            //Add ListView to the form.
            this.Controls.Add(listView1);
    
            //Search for a particular virtual item.
            //Notice that we never manually populate the collection!
            //If you leave out the SearchForVirtualItem handler, this will return null.
            ListViewItem lvi = listView1.FindItemWithText("111111");
    
            //Select the item found and scroll it into view.
            if (lvi != null)
            {
                listView1.SelectedIndices.Add(lvi.Index);
                listView1.EnsureVisible(lvi.Index);
            }
        }
    
        //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());
            }
        }
    
        //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());
            }
    
        }
    
        //This event handler enables search functionality, and is called
        //for every search request when in Virtual mode.
        void listView1_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e)
        {
            //We've gotten a search request.
            //In this example, finding the item is easy since it's
            //just the square of its index.  We'll take the square root
            //and round.
            double x = 0;
            if (Double.TryParse(e.Text, out x)) //check if this is a valid search
            {
                x = Math.Sqrt(x);
                x = Math.Round(x);
                e.Index = (int)x;
                   
            }
            //If e.Index is not set, the search returns null.
            //Note that this only handles simple searches over the entire
            //list, ignoring any other settings.  Handling Direction, StartIndex,
            //and the other properties of SearchForVirtualItemEventArgs is up
            //to this handler.
        }
    }
    
    Public Class Form1
        Inherits Form
        Private myCache() As ListViewItem 'array to cache items for the virtual list
        Private firstItem As Integer 'stores the index of the first item in the cache
        Private WithEvents listView1 As ListView
    
        Public Shared Sub Main()
            Application.Run(New Form1)
        End Sub
    
        Public Sub New()
            'Create a simple ListView.
            listView1 = New ListView()
            listView1.View = View.SmallIcon
            listView1.VirtualMode = True
            listView1.VirtualListSize = 10000
    
            'Add ListView to the form.
            Me.Controls.Add(listView1)
    
            'Search for a particular virtual item.
            'Notice that we never manually populate the collection!
            'If you leave out the SearchForVirtualItem handler, this will return null.
            Dim lvi As ListViewItem = listView1.FindItemWithText("111111")
    
            'Select the item found and scroll it into view.
            If Not (lvi Is Nothing) Then
                listView1.SelectedIndices.Add(lvi.Index)
                listView1.EnsureVisible(lvi.Index)
            End If
    
        End Sub
    
        '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
    
        '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
    
        'This event handler enables search functionality, and is called
        'for every search request when in Virtual mode.
        Private Sub listView1_SearchForVirtualItem(ByVal sender As Object, ByVal e As SearchForVirtualItemEventArgs) Handles listView1.SearchForVirtualItem
            'We've gotten a search request.
            'In this example, finding the item is easy since it's
            'just the square of its index.  We'll take the square root
            'and round.
            Dim x As Double = 0
            If [Double].TryParse(e.Text, x) Then 'check if this is a valid search
                x = Math.Sqrt(x)
                x = Math.Round(x)
                e.Index = Fix(x)
            End If
            'Note that this only handles simple searches over the entire
            'list, ignoring any other settings.  Handling Direction, StartIndex,
            'and the other properties of SearchForVirtualItemEventArgs is up
            'to this handler.
        End Sub
    
    End Class
    

    설명

    설정 합니다 VirtualMode 속성을 true 배치를 ListView 가상 모드에 있습니다.Setting the VirtualMode property to true puts the ListView into virtual mode. 일반 가상 모드로 Items 컬렉션이 사용 되지 않습니다.In Virtual mode, the normal Items collection is unused. 대신 ListViewItem 개체를 동적으로 생성 됩니다는 ListView 해야 합니다.Instead, ListViewItem objects are created dynamically as the ListView requires them.

    가상 모드는 여러 가지 상황에서 유용할 수 있습니다.Virtual mode can be useful under many circumstances. 경우는 ListView 메모리에 이미 매우 큰 컬렉션에서 개체를 입력 해야 만들기는 ListViewItem 개체 각 항목에 낭비 될 수 있습니다.If a ListView object must be populated from a very large collection already in memory, creating a ListViewItem object for each entry can be wasteful. 가상 모드에 필요한 항목에 대해서만 생성 됩니다.In virtual mode, only the items required are created. 다른 경우의 값에는 ListViewItem 개체를 자주 다시 계산 해야 할 수 있으며 전체 컬렉션에 대해이 작업을 수행 하면 성능이 생성 됩니다.In other cases, the values of the ListViewItem objects may need to be recalculated frequently, and doing this for the whole collection would produce unacceptable performance. 가상 모드에 필요한 항목에만 계산 됩니다.In virtual mode, only the required items are calculated.

    가상 모드를 사용 하려면 처리 해야 합니다는 RetrieveVirtualItem 때마다 발생 하는 경우는 ListView 항목이 필요 합니다.In order to use virtual mode, you must handle the RetrieveVirtualItem event, which is raised every time the ListView requires an item. 이 이벤트 처리기를 만들어야 합니다 ListViewItem 지정된 된 인덱스에 속하는 개체입니다.This event handler should create the ListViewItem object that belongs at the specified index. 또한는 VirtualListSize 가상 목록 크기 속성을 설정 해야 합니다.In addition, the VirtualListSize property must be set to the size of the virtual list.

    처리는 SearchForVirtualItem 이벤트를 사용 하면 가상 모드에서 검색 합니다.Handling the SearchForVirtualItem event enables searching in virtual mode. 이 이벤트 처리 되지 않은 경우는 FindItemWithText 하 고 FindNearestItem 메서드는 반환 null합니다.If this event is not handled, the FindItemWithText and FindNearestItem methods will return null.

    처리할 수 있습니다 합니다 CacheVirtualItems 캐시를 유지 하기 위해 이벤트 ListViewItem 개체입니다.You can handle the CacheVirtualItems event in order to maintain a cache of ListViewItem objects. 경우 계산을 만들려면 조회는 ListViewItem 개체 캐시에는 성능을 향상 시킬 수 유지 관리 비용이 많이 듭니다.If the calculation or lookup to create a ListViewItem object is expensive, maintaining a cache can improve performance.

    경우는 View 속성이 Tile를 값으로 자동으로 변경 됩니다 LargeIconVirtualMode 로 설정 된 true합니다.If the View property is set to Tile, the value will automatically be changed to LargeIcon when VirtualMode is set to true.

    가상 모드에는 Items 수집이 수행 되지 않습니다.In virtual mode, the Items collection is disabled. 결과에 액세스 하는 동안는 InvalidOperationException합니다.Attempting to access it results in an InvalidOperationException. 마찬가지 합니다 CheckedItems 컬렉션 및 SelectedItems 컬렉션입니다.The same is true of the CheckedItems collection and the SelectedItems collection. 선택 또는 선택 항목을 검색 하려는 경우 사용 합니다 SelectedIndicesCheckedIndices 컬렉션 대신 합니다.If you want to retrieve the selected or checked items, use the SelectedIndices and CheckedIndices collections instead.

    적용 대상