ListView VirtualMode

MiPakTeh 1,476 Reputation points
2021-03-07T04:33:56.827+00:00

Hi All,

What I try to do is,

  1. To see what file coming into my computer.Using Filesystemwatcher
  2. Use Listview virtual mode as control.

Here is code I trying;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Learning_12
{
    public partial class Form1 : Form
    {
        ListViewItem item1 = new ListViewItem("item1", 0);

        FileSystemWatcher Guarding = new FileSystemWatcher();

        public Form1()
        {
            InitializeComponent();

        }

        void OnChanged(object source, FileSystemEventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;

            listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView1_RetrieveVirtualItem);
            listView1.VirtualListSize = item1.SubItems.Count;
            listView1.VirtualMode = true;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.View = View.Details;
            listView1.GridLines = true;
            listView1.Columns.Add("Filename", 200);      // Creates column headings

            string sOri = @"C:\";

            Guarding.Path = sOri;
            Guarding.IncludeSubdirectories = true;
            Guarding.Created += new FileSystemEventHandler(OnChanged);
            Guarding.EnableRaisingEvents = true;
        }

        void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
        {

            e.Item = item1;
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }


    }
}

Thank.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,641 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Tucker 5,851 Reputation points
    2021-03-07T14:49:16.903+00:00

    Simplifying what Virtual Mode means is it only creates rows in the listview for the visual items. With that said you need keep a list of the files that are created.

    The other thing is you should not add event handlers as a result of FileWatcher event. It will make the events fire for each file added. So first time the RetrieveVirtualItem event will fire once. Once a second file is added it will fire twice etc.

    Here is a simple example that should help

    public partial class Form1 : Form
    {
        List<string> filesAdded = new List<string>();
        FileSystemWatcher Guarding = new FileSystemWatcher();
    
        public Form1()
        {
            InitializeComponent();
    
        }
    
        void OnChanged(object source, FileSystemEventArgs e)
        {
            filesAdded.Add(e.FullPath);
            listView1.VirtualListSize = filesAdded.Count();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
    
            listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView1_RetrieveVirtualItem);
            listView1.VirtualMode = true;
            listView1.View = View.Details;
            listView1.GridLines = true;
            listView1.Columns.Add("Filename", 200);      // Creates column headings
    
            string sOri = @"C:\";
    
            Guarding.Path = sOri;
            Guarding.IncludeSubdirectories = true;
            Guarding.Created += new FileSystemEventHandler(OnChanged);
            Guarding.EnableRaisingEvents = true;
        }
    
        void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
        {
            var item = new ListViewItem();
            if (filesAdded.Count > 0)
            {
                item.Text = filesAdded[e.ItemIndex];
            }
    
            e.Item =  item;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
    
        }
    
    
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful