C# 可编辑listview

下面是源代码:

1.ListViewEX.cs 文件

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WinFormListviewAddContrl
{
    //msg=0x115   (WM_VSCROLL)    
    //msg=0x114   (WM_HSCROLL)  
    ///  

  
    ///   CListView   的摘要说明。  
    ///  
  
    public class ListViewEx : ListView
    {
        private TextBox m_tb;
        private ComboBox m_cb;

        public ListViewEx()
        {
            m_tb = new TextBox();
            m_cb = new ComboBox();
            m_tb.Multiline = true;
            m_tb.Visible = false;
            m_cb.Visible = false;
            this.GridLines = true;
            //this.CheckBoxes = false;
            this.FullRowSelect = true;
            this.Controls.Add(m_tb);
            this.Controls.Add(m_cb);
        }
        private void EditItem(ListViewItem.ListViewSubItem subItem)
        {
            if (this.SelectedItems.Count <= 0)
            {
                return;
            }
           
            Rectangle _rect = subItem.Bounds;
            m_tb.Bounds = _rect;
            m_tb.BringToFront();
            m_tb.Text = subItem.Text;
            m_tb.Leave += new EventHandler(tb_Leave);
            m_tb.TextChanged += new EventHandler(m_tb_TextChanged);
            m_tb.Visible = true;
            m_tb.Tag = subItem;
            m_tb.Select();
        }
        private void EditItem(ListViewItem.ListViewSubItem subItem, Rectangle rt)
        {
            if (this.SelectedItems.Count <= 0)
            {
                return;
            }

            Rectangle _rect = rt;
            m_cb.Bounds = _rect;
            m_cb.BringToFront();
            m_cb.Items.Add(subItem.Text);
            m_cb.Text = subItem.Text;
            m_cb.Leave += new EventHandler(lstb_Leave);
            m_cb.TextChanged += new EventHandler(m_lstb_TextChanged);
            m_cb.Visible = true;
            m_cb.Tag = subItem;
            m_cb.Select();
        }


        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {

                if (this.SelectedItems.Count > 0)
                {
                    //this.SelectedItems[0].BeginEdit();
                    ListViewItem lvi = this.SelectedItems[0];
                    EditItem(lvi.SubItems[0],new Rectangle(lvi.Bounds.Left,lvi.Bounds.Top,this.Columns[0].Width,lvi.Bounds.Height-2));
                }
            }
            base.OnKeyDown(e);
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            this.m_tb.Visible = false;
            this.m_cb.Visible = false;
            base.OnSelectedIndexChanged(e);
        }

        //protected override void OnDoubleClick(EventArgs e)
        //{
        //    Point tmpPoint = this.PointToClient(Cursor.Position);
        //    ListViewItem item = this.GetItemAt(tmpPoint.X, tmpPoint.Y);
        //    if (item != null)
        //    {
        //        if (tmpPoint.X > this.Columns[0].Width && tmpPoint.X < this.Width)
        //        {
        //            EditItem(1);
        //        }
        //    }

        //    base.OnDoubleClick(e);
        //}
        protected override void OnDoubleClick(EventArgs e)
        {
            Point tmpPoint = this.PointToClient(Cursor.Position);
            ListViewItem.ListViewSubItem subitem = this.HitTest(tmpPoint).SubItem;
            ListViewItem item = this.HitTest(tmpPoint).Item;
            if (subitem != null)
            {
                if (item.SubItems[0].Equals(subitem))
                {
                    EditItem(subitem, new Rectangle(item.Bounds.Left, item.Bounds.Top, this.Columns[0].Width, item.Bounds.Height-2));
                }
                else
                {
                    EditItem(subitem);
                }
            }

            base.OnDoubleClick(e);
        }

        protected override void WndProc(ref   Message m)
        {
            if (m.Msg == 0x115 || m.Msg == 0x114)
            {
                this.m_tb.Visible = false;
            }
            base.WndProc(ref   m);
        }

        private void tb_Leave(object sender, EventArgs e)
        {
            m_tb.TextChanged -= new EventHandler(m_tb_TextChanged);
            (sender as TextBox).Visible = false;
        }

        private void m_tb_TextChanged(object sender, EventArgs e)
        {
            if ((sender as TextBox).Tag is ListViewItem.ListViewSubItem)
            {
                (this.m_tb.Tag as ListViewItem.ListViewSubItem).Text = this.m_tb.Text;
            }
           
        }
        private void lstb_Leave(object sender, EventArgs e)
        {
            m_cb.TextChanged -= new EventHandler(m_lstb_TextChanged);
        }
        private void m_lstb_TextChanged(object sender, EventArgs e)
        {
            if ((sender as ListBox).Tag is ListViewItem.ListViewSubItem)
            {
                (this.m_cb.Tag as ListViewItem.ListViewSubItem).Text = this.m_cb.Text;
            }
        }
    }
}

转载请说明出处!!!谢谢
2.Form1.cs

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

namespace WinFormListviewAddContrl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            AddData();
        }
        public void AddData()
        {
            this.cListView1.Columns.Add("姓名",60, HorizontalAlignment.Left);
            this.cListView1.Columns.Add("性别", 60, HorizontalAlignment.Left);
            this.cListView1.Columns.Add("年龄", 60, HorizontalAlignment.Left);
            this.cListView1.Columns.Add("公司", 60, HorizontalAlignment.Left);
            this.cListView1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "23" ,"GANA"}));
            this.cListView1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "23", "GANA" }));
            this.cListView1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "23", "GANA" }));
            this.cListView1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "23", "GANA" }));

        }
    }
}
 
 

你可能感兴趣的:(C#,C,shape)