C # - Example of VirtualMode in ListView: Load the file list in the directory

Video C # - Example of VirtualMode in ListView: Load the file list in the directory

VirtualMode is a method for you to manage data in a ListView (or DataGridView) with the main purpose is to increase the speed displayed in those cases have to load too much data to the ListView. With this method, the data will not be added directly to the ListView that will be created when necessary to display. In this article I will make an example to load and search files in a folder on the ListView.

Download ListView VirtualMode Example (VC # 2010)

to introduce

To use this method, first you need to set the property VirtualMode by true . Then you need to assign property VirtualListSize equal number of elements that contain ListView.

A few points to note are the property of the ListView as Items, CheckedItems, SelectedItems will not be used anymore. Instead, please use CheckedIndices and SelectedIndices instead.

Also as to mode View as Tile , it will automatically switch to LargeIcon .

In this example, I created a class that inherits from the ListView Y2ListView to perform the necessary work. This class FolderPath property, when assigning a new value for this property, the ListView will be updated again to retrieve and display a list of files in that folder.

C # - Example of VirtualMode in ListView: Load the file list in the directory

Events RetrieveVirtualItem

In the VirtualMode, events RetrieveVirtualItem will be called when the ListView needs to take a certain element. You need to create and assign property element of the parameter Item RetrieveVirtualItemEventArgs. Based on ItemIndex property of this parameter, you can get the necessary value from the data source to create the element.

    public class Y2ListView:ListView
    {
        string[] _files;
        // data
        private string _folderPath;
        public string FolderPath
        {
            get {return _folderPath;}
            set
            {
                if(String.IsNullOrEmpty(value))
                    return;
                _folderPath=value;
                _files=Directory.GetFiles(_folderPath);
                Array.Sort(_files);
 
                this.VirtualListSize=_files.Length;
            }
        }
 
        public Y2ListView()
        {
            this.VirtualMode=true;
        }
        protected override void OnRetrieveVirtualItem(RetrieveVirtualItemEventArgs e)
        {
            string filePath=_files[e.ItemIndex];
            string fileName=Path.GetFileName(filePath);
            FileInfo finfo=new FileInfo(filePath);
 
            ListViewItem item=new ListViewItem(fileName,e.ItemIndex-_startIndex);
 
            double size=Math.Ceiling(finfo.Length/1024f);
            item.SubItems.Add(size.ToString("#,0 KB"));
            item.SubItems.Add(finfo.LastWriteTime.ToString());
 
            e.Item=item;
 
            base.OnRetrieveVirtualItem(e);
        }
 
    }
}

Because ListView that I create will have three columns Name, Size, so in the event DateModified RetrieveVirtualItem, you also need to make the item available in three values ​​(sub item).

Events CacheVirtualItems

You can handle this event to speed up the ListView in VirtualMode regime. By creating and storing necessary data available is going to be used, the retrieval and display elements will be faster.

For example, I added an ImageList to display icons for files. Instead of writing directly in events RetrieveVirtualItem, I will store necessary icons available in the event ImageList CacheVirtualItems:

protected override void OnCacheVirtualItems(CacheVirtualItemsEventArgs e)
{
    _imageList.Images.Clear();
    for (int i = e.StartIndex; i < e.EndIndex; i++)
    {
        Icon icon=Icon.ExtractAssociatedIcon(_files[i]);
        _imageList.Images.Add(icon);
    }
    base.OnCacheVirtualItems(e);
}

Based on StartIndex property and endIndex, I just grab the icon of the file in this space and save on ImageList. Thus there will be endIndex-StartIndex ImageList element from 0, so we need a variable to store the value StartIndex if you want to assign to the ListViewItem through ImageIndex image.

Events SearchForVirtualItem

This event should be handled if you want to use two methods FindItemWithText  and  FindNearestItem  of ListView. Parameters SearchForVirtualItemEventArgs property contains the text as the value of the search string (in this example, the file name including the extension), and Index is the value that you need to assign it to determine the elements found in list book. Because the list of the files have been sorted, I use BinarySearch to realize this event simply as follows:

protected override void OnSearchForVirtualItem(SearchForVirtualItemEventArgs e)
{
    string filePath=Path.Combine(_folderPath,e.Text);
    int index=Array.BinarySearch(_files,filePath);
 
    if(index>-1)
        e.Index=index;
    base.OnSearchForVirtualItem(e);
}

Complete source code class Y2ListView

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
 
namespace Win1
{
    public class Y2ListView:ListView
    {
        private ImageList _imageList;
        int _startIndex;
 
        string[] _files;
 
        private string _folderPath;
        public string FolderPath
        {
            get {return _folderPath;}
            set
            {
                if(String.IsNullOrEmpty(value))
                    return;
                if(_imageList!=null)
                    _imageList.Images.Clear();
 
                _startIndex=-1;
                _folderPath=value;
                _files=Directory.GetFiles(_folderPath);
                Array.Sort(_files);
 
                this.VirtualListSize=_files.Length;
            }
        }
 
        public Y2ListView()
        {
            _imageList=new ImageList();
            _imageList.ImageSize = new System.Drawing.Size(16, 16);
 
            this.VirtualMode=true;
            this.DoubleBuffered=true;
            this.SmallImageList=_imageList;
            this.LargeImageList=_imageList;
 
        }
        protected override void OnSearchForVirtualItem(SearchForVirtualItemEventArgs e)
        {
            string filePath=Path.Combine(_folderPath,e.Text);
            int index=Array.BinarySearch(_files,filePath);
            if(index>-1)
                e.Index=index;
            base.OnSearchForVirtualItem(e);
        }
        protected override void OnCacheVirtualItems(CacheVirtualItemsEventArgs e)
        {
            if(_startIndex==e.StartIndex)
                return;
            _startIndex=e.StartIndex;
            _imageList.Images.Clear();
            for (int i = e.StartIndex; i < e.EndIndex; i++)
            {
                Icon icon=Icon.ExtractAssociatedIcon(_files[i]);
                _imageList.Images.Add(icon);
            }
            base.OnCacheVirtualItems(e);
        }
 
        protected override void OnRetrieveVirtualItem(RetrieveVirtualItemEventArgs e)
        {
            string filePath=_files[e.ItemIndex];
            string fileName=Path.GetFileName(filePath);
            FileInfo finfo=new FileInfo(filePath);
 
            ListViewItem item=new ListViewItem(fileName,e.ItemIndex-_startIndex);
 
            double size=Math.Ceiling(finfo.Length/1024f);
            item.SubItems.Add(size.ToString("#,0 KB"));
            item.SubItems.Add(finfo.LastWriteTime.ToString());
 
            e.Item=item;
 
            base.OnRetrieveVirtualItem(e);
        }
    }
}

https://yinyangit.wordpress.com

The hidden indicators of players in FIFA Online 4

The hidden indicators of players in FIFA Online 4

The hidden traits of players in FIFA Online are quite important factors, not only contributing to the perfection of the players but also the factors that help create a perfect squad.

MFC - Lesson1: Hello World!

MFC - Lesson1: Hello World!

This is the first article in the series about programming guide VC ++ 6 with MFC for beginners. Through simple examples, each article will give you the basics so you can get to work on application development using VC ++.

Mission and target system in Temple Run 2

Mission and target system in Temple Run 2

To master this game, you need to understand the system of tasks and goals that you need to complete in Temple Run 2.

Summary of weapons in Garena Free Fire

Summary of weapons in Garena Free Fire

Weapons are always something that gamers care about when playing shooting games or survival games. The same goes for Garena Free Fire.

Html5-Canvas: Zoom in / zoom out the game with the mouse wheel

Html5-Canvas: Zoom in / zoom out the game with the mouse wheel

This article shall comply with your request xtranfor55  in article Scroll background image and map (Map Scrolling) . I'll describe how to do this by 2 ways: using the Canvas API and processed manually in code.

How to avoid detection when using the PUBG Mobile emulator on PC

How to avoid detection when using the PUBG Mobile emulator on PC

Recently, PUBG Mobile announced to gamers that PUBG Mobile players using Android emulator on computer will only be able to play with each other without those who play with phone. After the PUBG Mobile gaming community complained those people

Laplace M: Summary of 6x gold weapons and specific stats

Laplace M: Summary of 6x gold weapons and specific stats

Images, information and indexes on the level 60 golden weapons of each class in Laplace M.

Instructions for loading Yong Heroes game card

Instructions for loading Yong Heroes game card

If you are a beginner to playing Yong Heroes and do not know how to deposit money into this game, the following article will guide us to load Yong Heroes game card, with just a few simple steps.

How to download and install FIFA Online 3 game

How to download and install FIFA Online 3 game

FIFA Online 3 must be a name that is no stranger to gamers, especially football lovers. FO3 allows you to participate in intense online matches with friends. To play FO3, you need to download and install additional software Garena P

How to earn recovery items in BTS World

How to earn recovery items in BTS World

Help the members of BTS recover after completing a series of missions from the agency by earning valuable restoration items in the game BTS World.

Information about all Quests in EverWing

Information about all Quests in EverWing

In EverWing, you have many ways to earn gold, items, trophies and experience. However, there is another way for you to plow items, money and experience. It is to perform a task (Quest) in EverWing, so your accurate information

How to create a WTFast account on your computer

How to create a WTFast account on your computer

To use WTFast to speed up gaming, gamers need to ensure two basic and mandatory conditions, that is, to install WTFast on the computer and have a WTFast account activated.

PUBG Mobile: Time slots and locations for Flare Gun appear on Sanhok

PUBG Mobile: Time slots and locations for Flare Gun appear on Sanhok

If you want to earn Flare Gun when playing PUBG Mobile, especially on Sanhok, you need to pay attention to the following.

PUBG Mobile: Some useful tips when playing on Sanhok map

PUBG Mobile: Some useful tips when playing on Sanhok map

Here are useful tips and information that will help you spend TOP 1 easily when participating in Sanhok map

PUBG: The location of the roller coaster map Erangel and Miramar

PUBG: The location of the roller coaster map Erangel and Miramar

Summary of the locations of gliders in PUBG in Erangel and Miramar maps for gamers who love to fly.

How to reduce lag effectively when playing PUBG Mobile

How to reduce lag effectively when playing PUBG Mobile

With the phone configuration weak when playing games often occurs jerky, lag. In particular, PUBG Mobile developed on relatively high graphics platform, requiring the device to be strong enough for a smooth new experience.

PUBG Mobile: 5 tips to track enemies from a distance for newbies

PUBG Mobile: 5 tips to track enemies from a distance for newbies

Tracking enemies from afar probably is not easy for new players joining PUBG Mobile. We must have keen eyes and constantly grasp the position of the opponents who are near us to take control of the situation.

PUBG Mobile: The most mapable and battleable locations in Sanhok

PUBG Mobile: The most mapable and battleable locations in Sanhok

These are the locations with lots of items and many players parachuting into the Sanhok map of PUBG Mobile

Simple and easy ways to create mind maps

Simple and easy ways to create mind maps

Mind map is one of the ways to help you create, remember and plan for all effective ideas and projects. The following article will guide you on how to create mind maps easily.

PUBG: Synthesize the hottest skydiving locations in Sanhok map

PUBG: Synthesize the hottest skydiving locations in Sanhok map

The area of ​​Sanhok is 4x4km, only 1/4 compared to Erangel and Miramar, with such a small area, but PUBG still does not reduce the number of players in the game, so the game will be very fast and will happen. painting continuously, so the weapon will appear