Virtual ListViews - Name for Speed
ListView. Huh? Come on, you all know it - the right pane of Windows Explorer. That is, where contents of folders are displayed. Whenever you see large icons, or small items listed in columns, or a list of items with details in other columns, it is always one and the same control - ListView. Kind of a Listbox, but much, much better and powerful. ListView control came into this world with Windows Explorer back in WIndows 95, and got numerous enhancements eversince. ListView is very convenient when it comes to display a list of items. It can even substitute grid controls for read-only data in many cases, as ListView can display multiple columns per data item. Add support for icons, check boxes, grouping and different layouts, and you have a real beauty. How to use ListViews is out of the scope of this post. If you want to learn it - just google for it. There are tons of tutorials out there.
Great as they are, ListView contols can be very slow with large number of items (like tens of thousands), especially while populating with data. Furthermore, by default ListView stores all its items internally. In case you populate the List View from an in-memory collection, you end up with doubled memory usage. Another thing to be worried about when working with tons of data.
The solution to both the performance and memory-usage issues is to use Virtual ListViews.
In this article I will not go into details about how to use virual list views in a generic Win32 application. I will make my examples in Windows Forms. Win32 and Delphi fans, do stay disappointed. There is nothing actually different in using Virtual ListViews in any platform. You either respond to messages (Win32 fashion), or attach to events (Delphi and NET). Anyways, you are expected to do basically the same thing.
ListView controls support a special operating mode - virtual mode. In this mode, the list view control does not store the items it displays internally. In fact, it only stores the number of items it contains, and nothing else. Then, when it needs to display an item, it simply sends a message to its parent window to provide the actual data of the item - caption, columns, icon index. As a result, the listview doesn’t consume any memory, and it is very fast to initialize it - you simply don’t populate it with data.
To make a ListView control virtual, set property VirtualMode to true, and set proprery VirtualListSize to the number of elements in your collection with data.
The minimal functionality you have to provide is to add an event handler to LisiView’s RetrieveVirtualItem event. A simple code would go something like this:
Though being clearly obvious, let me explain. MyDataClass is a custom class I defined, which happens to have two properties of type String - Title and Description. Of course, you are not limited or required to have such; you can have anything you need. data is a collection of my data (I can’t believe I typed this, but my experience at the university taught me better).
Basically, you are done.
Virtual ListViews are even more powerful than that. You can cache your data for improved performance, and handle extra events to support searching. If I got you interested, just take a look at MSDN.
February 14th, 2010 at 2:51 pm
Hi everyone. I have surfed to your site accidentally and can not break away from it. It is really great. Help me! Could you help me find sites on the: Phenergan necrosis. I found only this - phenergan drug test. Phenergan, mf is already considering with the real potential. Addie, she wakes to leave clear doing likely, phenergan. Best regards :o, Rodney from Denmark.