如何在UserControl中创建costum listview控件?

时间:2016-03-01 15:39:29

标签: c# .net winforms

我想要做的是当我从工具箱中拖动form1设计器中的UserControl时,我将能够使用/控制ListView控件。

这是UserControl代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test
{
    public partial class lvnf : UserControl
    {
        ListViewNF lvnfs;

        public lvnf()
        {
            InitializeComponent();

            lvnfs = new ListViewNF();
            lvnfs.Location = new Point(50, 50);
            lvnfs.Size = new Size(50, 50);
            lvnfs.View = View.Details;
            lvnfs.Dock = DockStyle.Fill;
            lvnfs.SuspendLayout();
            lvnfs.LabelEdit = true;
            lvnfs.Columns.Add("From", 100, HorizontalAlignment.Left);
            lvnfs.Columns.Add("Subject", 200);
            lvnfs.Columns.Add("Date", 300);
            lvnfs.Sorting = SortOrder.None;
            //lvnf.ColumnClick += lvnf_ColumnClick;
            //lvnf.Click += lvnf_Click;
            //lvnf.SelectedIndexChanged += lvnf_SelectedIndexChanged;
            this.Controls.Add(lvnfs);
            lvnfs.ResumeLayout(false);
        }

        class ListViewNF : System.Windows.Forms.ListView
        {
            public ListViewNF()
            {
                //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(System.Windows.Forms.Message m)
            {
                //Filter out the WM_ERASEBKGND message
                if (m.Msg != 0x14)
                {
                    base.OnNotifyMessage(m);
                }
            }
        }

        private void ListViewNFTest_Load(object sender, EventArgs e)
        {

        }
    }
}

问题是当我将UserControl拖动到form1 desginer时我可以使用lvnf,但它是UserControl而不是ListViewNF控件。我希望能够在UserControl中使用ListViewNF控件。

我想将ListViewNF命名为lvnf所以当我将UserContro拖到form1设计器时,我将能够使用lvnf(ListView控件)而不是UserControl。

0 个答案:

没有答案