VB.NET - Someone give me a guide on Virtual Mode (Listview Control) ?

Asked By Gavin
20-Dec-10 05:13 AM
I have been looking around for a tutorial on Virtual mode for listview on VB.Net, but I can't find one that explains clear enough or simple to follow in VB.Net.
  Reena Jain replied to Gavin
20-Dec-10 05:23 AM
hi,

XAML Controls(WPF) have some features that support faster loading by few virtualization methods. 

However, interestingly, a ListView control has another mode called the "Virtual Mode", which allows the ListViewITem objects to be  generated dynamically instead of being stored in the collection. To enable Virtual mode for a ListView, set its VirtualMode property to true. But note that you would have to handle the RetrieveVirtualItem, CacheVirtualItems, and SearchForVirtualItem events.

Additionally, things to note are the limitations of the Virtual Mode: 
Tile View – if you are in Tile view in the ListView, it will change to LargeIcons instead.
SelectedItems/CheckedItems – these properties are not supported. Instead, use the SelectedIndices/ChecjedIndices properties to figure out which items exist in those collections.
Changing amount of sub items in a ListViewItem instance – when creating ListViewItem instances, you need to make sure that it has the appropriate amount of ListViewSubItem instances in it. With regular list view, you can omit some of them and they will appear empty in the ListView.
ArrangeIcons() – this method is not supported.
Groups – you cannot use groups in a virtual ListView.
Positioning of items – you cannot position ListViewItems in any of the icon modes.
Automatic sorting – you cannot use the Sort() method on the ListView or any associated operations.
detail is available in http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx MSDN article 

An easier alternative would be use other controls like the VirtualObjectListView. http://www.codeproject.com/KB/list/ObjectListView.aspx Code Project article talks on it.

hope this will help you
  Gavin replied to Reena Jain
20-Dec-10 05:27 AM
Thanks a lot Reena. Been searching forever.......
  Danasegarane Arunachalam replied to Gavin
20-Dec-10 05:30 AM
Setting the VirtualMode property to true puts the ListView into virtual mode. In Virtual mode, the normal Items collection is unused. Instead, ListViewItem objects are created dynamically as the ListView requires them.

Virtual mode can be useful under many circumstances. 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. 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.

In order to use virtual mode, you must handle the RetrieveVirtualItem event, which is raised every time the ListView requires an item. This event handler should create the ListViewItem object that belongs at the specified index. In addition, the VirtualListSize property must be set to the size of the virtual list.

Handling the SearchForVirtualItem event enables searching in virtual mode. If this event is not handled, the FindItemWithText and FindNearestItem methods will return nullNothingnullptra null reference (Nothing in Visual Basic).

You can handle the CacheVirtualItems event in order to maintain a cache of ListViewItem objects. If the calculation or lookup to create a ListViewItem object is expensive, maintaining a cache can improve performance.

If the View property is set to Tile, the value will automatically be changed to LargeIcon when VirtualMode is set to true.

In virtual mode, the Items collection is disabled. Attempting to access it results in an InvalidOperationException. The same is true of the CheckedItems collection and the SelectedItems collection. If you want to retrieve the selected or checked items, use the SelectedIndices and CheckedIndices collections instead.


Example:

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
  Gavin replied to Danasegarane Arunachalam
20-Dec-10 05:31 AM
Thanks for the reply, the reason I wanted to do virtual mode is because it lags in the normal way of loading huge lists.
welcome gavin:)  welcome gavin:)
20-Dec-10 07:31 AM
end of post
Create New Account
help
ListView control, virtual mode, ListViewItems shown .NET Framework Hi, How to get a list of ListViewItems that are currently shown in a Windows Form ListView control when that that control is in a virtual mode? C# Discussions ListView (1) GetEnumerator (1) ListViewItems (1) Windows (1) Enumerator (1) Indexer (1) Sorry, I was confused by the message: ListView items collection using an enumerator or call GetEnumerator. Use the ListView items indexer instead and
Redraw flashing problem with ListView in virtual mode. .NET Framework I am using the .Net 2.0 ListView control in virtual mode and am having some unsightly re-draw / flashing problems. The listview control works quite well in this mode when displaying and scrolling thousands of items. The re-draw problem occurs when I insert
lange .NET Framework Hallo Ich komme irgendwie nicht weiter. . . Ich habe eine form mit einem listview im virtual mode. Dann habe ich ein Interface um auf eine compact 3.5 datenbank zuzugreifen in dem die linq klassen in die Schnitstelle nehmen. Ist da an der vorgehensweise was falsch? Das ListView braucht ja nur maximal 30 datensätze abfragen und braucht dafür über 20sec. . . Code wollte ich aber wenn er gewünscht wird schreibe ich die entscheidenen zeilen mal raus. C# - German Discussions ListView.CacheVirtualItems (1) Database.Personen.GetCount (1) Database.Personen.GetItem (1) System.Windows.Forms (1) ListView.VirtualMode (1) ListViewItem (1) Hallo Florian, na, Du sagsts, die "Abfrage" dauert 20 Sekunden. Also class Database { public static iPerson Personen ; public void setDatabase() { Personen = Anbindung.Connection.T_Person; } } um das ListView zu behandeln wird beim laden LV.VirtualListSize = Database.Personen.GetCount(); und im RetrieveVirtualItem event { iPerson cachen (oder über CacheVirtualItems) und dann bei RetrieveVirtualItem aus dem Cache lesen. Hier ein Beispiel: [ListView.VirtualMode-Eigenschaft (System.Windows.Forms)] http: / / msdn.microsoft.com / de-de / library / system.windows.forms
Virtual ListView with Checkboxes. Is it possible? .NET Framework I have a virtual ListView (VirtualMode set to true). To make it more convenient for the users of the ListView to select its items, I've set the ListView's CheckBoxes property to true. However, in virtual mode no checkboxes appear. That is, when the View style is set to Details the text