4

I have a ListView and ImageList in C# on my form and read a directory with about 1000 files maximum. I pre-populate the ListView and ImageList with the count of the fileItems DummyItems with the AddRange methods to avoid the flickering and scolling ListView.

Now in the second step I just wanted to assign the right item information to the dummy items while I read the real items from file system. Item text is sofar no problem, but I can't replace the dummy images. It throws always an invalid argument exception if I try to do so. To delete the image with RemoveAtIndex or RemoveAtKey and then re-add would take me ages to iterate through 1000 files. 1000 files take 8 minutes with "RemoveAtKey" in ImageList. "RemoveAtKey" is the bottleneck which I found out. If I try to clear all Images before and re-populate with AddRange again my Item Images go blank or a exception occurs. Does someone know how I get 1000 different thumbnails from 1000 files with the file name fast into a listview control with another methods than I use?

feedwall
  • 1,473
  • 7
  • 28
  • 48

1 Answers1

0

First of all you might want to create a new user control named 'ListViewNF', with the following code:

class ListViewNF : System.Windows.Forms.ListView
{
    public ListViewNF()
    {
        //Activate double buffering
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);

        //Enable the OnNotifyMessage event so we get a chance to filter out 
        // Windows messages before they get to the form's WndProc
        this.SetStyle(ControlStyles.EnableNotifyMessage, true);
    }

    protected override void OnNotifyMessage(Message m)
    {
        //Filter out the WM_ERASEBKGND message
        if(m.Msg != 0x14)
        {
            base.OnNotifyMessage(m);
        }
    }
}

This fixes the flickering issues when adding items to a list view at high speeds.

I'm still doing some research and tests for your other issue.

Swen Kooij
  • 985
  • 1
  • 8
  • 18