Search code examples
c#listviewcustom-controls

use custom control directly in Visual Studio project


i want to use the listview flicker"less" control found here Link

directly in my c# Project. i dont want to make a custom user control project, build it to dll and then import it in my project. i just want this all in my c# Programm i am making.

i think i have to add in my project a class, and add the code, but how can i use the control now directly in my project?


Solution

  • 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.