listview中item与label交互使用

这个博客介绍了如何在ListView控件中实现Item与Label的交互操作,特别是双击编辑功能。通过创建一个自定义的EnhanceListView类,扩展了ListView的功能,包括允许编辑、跟踪鼠标位置、获取编辑框位置等。当用户双击ListView项的某个子项时,会弹出一个TextBox用于编辑文字,完成编辑后更新ListView中的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值