Completely Solve .NET ListView Flickering Problem
Posted by Admin L in .NET Programming on 06-04-2012. Tags: .NET Programming Experience
Author: Nosa Lee
Original Address: https://www.seeksunslowly.com/dot-net-listview-flicker-resolve
To reprint this article, please indicate the source, thank you.
_____________________________________
When I add, delete or modify ListView’s data (Items) with a high-frequency, the ListView control will flicker.
It makes my software to look very amateur, also dazzles myself. So, I must solve the problem.
Tried to use some functions of ListView, such as BeginUpdate, EndUpdate, SuspendLayout, ResumeLayout, Refresh, and their combination, No perfect solution found.
I had used Delphi several years ago, vaguely remembered TListView has the DoubleBuffer property, so, tried to implement a class that inherits from ListView, and add the DoubleBuffer setting to its constructor, and then test it.
The result exceeds the expectation due to get success only one time.
The code of the new class TListView that inherits from ListView class of .NET as below (VB 2008):
[cc lang=”vbnet”]
Imports System.Windows.Forms
Public Class TListView
Inherits ListView
Public Sub New()
‘ Use DoubleBuffer to avoid flickering when operate TListView items in high-frequency.
SetStyle(ControlStyles.DoubleBuffer Or _
ControlStyles.OptimizedDoubleBuffer Or _
ControlStyles.AllPaintingInWmPaint, True)
UpdateStyles()
End Sub
End Class
[/cc]
If you use C#, I provide you with the C# code also:
[cc lang=”csharp”]
class TListView : ListView
{
public DoubleBufferListView()
{
SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
}
}
[/cc]
Note:
1. To apply TListView to your existing projects, only need to replace System.Windows.Forms.ListView (usually generated by .NET automatically) as TListView (of course, you may have used a namespace for this new class) in the code files.
2. I test it successfully in TListView with the Details view (usually display bulk data in this view).
3. I estimate this thinking is also applicable for TreeView (have not tested), if you need that, just try this.
Yepp, this works as a charm. 🙂
I have high hopes for this as I don’t want to recreate a long project but I get the following exception. I have tried this in Designer using:
As DBListView
As XBeeAPIClient.DBListView
I get this exception?
An unhandled exception of type ‘System.InvalidOperationException’ occurred in XBeeAPIClient.exe
Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: Unable to cast object of type ‘System.Windows.Forms.ListView’ to type ‘XBeeAPIClient.DBListView’.
Imports System.Windows.Forms
Public Class DBListView
Inherits ListView
Public Sub New()
SetStyle(ControlStyles.DoubleBuffer Or _
ControlStyles.OptimizedDoubleBuffer Or _
ControlStyles.AllPaintingInWmPaint, True)
UpdateStyles()
End Sub
End Class