C# – Flickering in ListView control (OwnerDraw, Virtual)

cflickerlistviewownerdrawnwinforms

This question might be considered a follow-up to Flickering in listview with ownerdraw and virtualmode.

I've got a ListView control in Virtual mode and I attempt to perform custom drawing. Item rendering is done via the following method override:

protected override void OnDrawItem(DrawListViewItemEventArgs eventArgs) 

As mentioned in the referenced question, custom drawing introduces flickering on mouse over events. Debugger tells me this happens due to an excessive amount of custom draw events which are fired.


Now – the accepted answer to the referenced question tells us:

This is a bug in .NET's ListView and you cannot get around it by
double buffering.

  • So, how reliable is that information? Is that really a bug? Or maybe we simply attempt to cut off a part of the messages and hope that it won't alter the visible behavior?

  • Is this true that if I have my owner drawing routine for the ListView in Virtual Mode, I can suppress these Custom Draw events and only perform my drawing in WM_PAINT or, maybe, this is incorrect for some cases?

  • What are the prerequisities for the System.Windows.Forms control to be able to do all the painting in WM_PAINT without altering it's initial behavior?

Best Answer

At least for double buffering for OnDrawItem, it is incorrect that there is a bug, but it is a little bit stupid: there is a protected attribute you can set, but you need to override the ListView. I created this kind of class:

public class MyListView : ListView
{
    public MyListView()
        : base()
    {
        DoubleBuffered = true;
    }
}

And then in my MyForm.Designer.cs file I change the instantiation of the ListView with the following line:

private ListView myListView;

this.myListView = new MyListView();

And OnDrawItem will work like a charm!

Related Topic