I have an inherited listview and there is significant flickering when I click column headers. The list is in details view.
public ListViewEx()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
this.DoubleBuffered = true;
}
int sortColumn = -1;
protected override void OnColumnClick(ColumnClickEventArgs e)
{
if(e.Column != sortColumn)
{
sortColumn = e.Column;
this.Sorting = SortOrder.Ascending;
}
else
{
if(this.Sorting == SortOrder.Ascending)
this.Sorting = SortOrder.Descending;
else
this.Sorting = SortOrder.Ascending;
}
this.Sort();
}
There is no flickering when I populate the list.
for(int i = 0; i < 10; i++)
{
ListViewItem lvi = new ListViewItem("this is column 1 " +i);
lvi.SubItems.Add("...
lvi.SubItems.Add("...
lvi.SubItems.Add("...
lvi.SubItems.Add("...
lvi.SubItems.Add("...
listViewEx1.Items .Add (lvi);
}
Edit
WM_ERASEBKGND didn't solve my problem.
I added this code at the form hosting the listview and the flicker is gone
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}