In Visual Studio, right-click on your project and then click ADD | USER CONTROL
. Name the new control ListViewNF
and click ADD
.
View the code for the new class. Change this line:
public partial class ListViewNF : UserControl
to this:
public partial class ListViewNF : ListView
and Rebuild. You'll get a compiler error about AutoScaleMode
- just delete the line in InitializeComponent
that's causing the error:
// delete this line:
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
So far, your code will look like this:
public partial class ListViewNF : ListView
{
public ListViewNF()
{
InitializeComponent();
}
}
Change it to this:
public partial class ListViewNF : ListView
{
public ListViewNF()
{
InitializeComponent();
//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);
}
}
}
Rebuild your project, and you should now see the ListViewNF
in your Toolbox of controls on the left (right at the top). You can drag this control onto a form in the designer, just like a regular ListView
.