关闭

listview中item与label交互使用

920人阅读 评论(0) 收藏 举报

listview中item与label交互使用方法

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

namespace EnhanceListView
{
    public partial class EnhanceListView : ListView
    {
        private TextBox inputBox = new TextBox();   // 输入编辑框
        private bool allowEdit = false;             // 是否允许编辑
        private Point mousePt = new Point(0,0);     // 当前鼠标的位置
        private int selectIndex = -1;               // 当前选中的行
        private int subIndex = -1;                  // 当前选择子项的索引
        private int Offset = 4;                     // 偏移量
        private int itemHeight = 13;

        private const Int32 WM_VSCROLL = 0x115;
        private const Int32 WM_HSCROLL = 0x114;

   
        public EnhanceListView ()
        {
            InitializeComponent();
            this.LabelEdit = false;

            inputBox.BorderStyle = BorderStyle.None;
            inputBox.Multiline = true;
            inputBox.BackColor = Color.LightGray;

            inputBox.LostFocus += new EventHandler( inputBox_LostFocus );
           
        }

        void inputBox_LostFocus ( object sender, EventArgs e )
        {
            this.Items[this.selectIndex].SubItems[this.subIndex].Text = this.inputBox.Text;
            this.Controls.Remove( inputBox );
        }

        protected override void WndProc ( ref Message m )
        {
            if ( m.Msg == WM_VSCROLL || m.Msg == WM_HSCROLL )
            {
                this.Controls.Remove( inputBox );
            }
            base.WndProc( ref m );
        }

        [Browsable(true)]
        public bool AllowEdit
        {
            get
            {
                return allowEdit;
            }
            set
            {
                allowEdit = value;
            }
        }


        /// <summary>
        /// 取得当前显示BOX的位置
        /// </summary>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        private Point getBoxPosition ( int startIndex )
        {
            int x = 0;
            int y = 0;

            this.itemHeight = (int)this.Font.Size + 4;

            y = 17 + ( this.selectIndex - startIndex ) * itemHeight;


            int xx = this.mousePt.X - this.GetItemAt( this.mousePt.X, this.mousePt.Y ).Position.X;

            for ( int i = 0; i < this.Columns.Count; i++ )
            {
                if ( x + this.Columns[i].Width > xx )
                {
                    this.subIndex = i;
                    break;
                }

                x = x + this.Columns[i].Width;
            }

            x = x + 1 + this.GetItemAt( this.mousePt.X, this.mousePt.Y ).Position.X - this.Offset;
            y = y + this.Offset;

            return new Point(x , y );
        }       

        /// <summary>
        /// 双击编辑事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDoubleClick ( EventArgs e )
        {
            base.OnDoubleClick(e);

            if ( !this.allowEdit )
                return;

            int headerIndex = this.TopItem.Index;                         // 获取当前最顶级的item索引

            this.inputBox.Location = this.getBoxPosition( headerIndex );

            if ( this.Items[this.selectIndex].SubItems.Count <= this.subIndex )
            {
                return;
            }

            this.inputBox.Width = this.Columns[this.subIndex].Width - 2;
            this.inputBox.Height = this.itemHeight -1;
            this.inputBox.Text = this.Items[this.selectIndex].SubItems[this.subIndex].Text;
            this.Controls.Add( inputBox );
            this.inputBox.Focus();
        }

        /// <summary>
        /// 鼠标单击
        /// 记录坐标, 选择项
        /// 右键弹出菜单
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown ( MouseEventArgs e )
        {
            this.mousePt = new Point(e.X, e.Y);   // 设置当前鼠标的位置

            if ( this.GetItemAt( this.mousePt.X, this.mousePt.Y ) != null )
            {
                this.selectIndex = this.GetItemAt( this.mousePt.X, this.mousePt.Y ).Index;   // 当前项的索引
            }


            if ( e.Button == MouseButtons.Right )
            {
                if ( this.selectIndex == -1 )   // 未选中任何项
                {
                    this.toolStripMenuItemRemove.Enabled = false;
                }

                else
                {
                    this.toolStripMenuItemRemove.Enabled = true;
                }

                this.contextMenuStrip.Show( this, this.mousePt );
            }

            base.OnMouseDown( e );
        }

        protected override void OnColumnWidthChanged ( ColumnWidthChangedEventArgs e )
        {
            base.OnColumnWidthChanged( e );
            this.Controls.Remove( inputBox );
        }

        private void contextMenuStrip_ItemClicked ( object sender, ToolStripItemClickedEventArgs e )
        {
            if ( e.ClickedItem.Name == "toolStripMenuItemAdd" )
            {
                string[] subitems = new string[this.Columns.Count];
                for ( int i = 0; i < this.Columns.Count; i++ )
                {
                    subitems[i] = "";
                }

                this.Items.Add( new ListViewItem( subitems ) );
            }

            else if ( e.ClickedItem.Name == "toolStripMenuItemRemove" )
            {
                if ( this.selectIndex == -1 )
                    return;

                this.Controls.Remove( this.inputBox );
                this.Items.RemoveAt(this.selectIndex);
            }
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27013次
    • 积分:419
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:11篇
    • 译文:0篇
    • 评论:4条
    我的链接