listview中item与label交互使用

原创 2006年05月26日 11:29:00

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

相关文章推荐

使用ListView填充Dialog对话框里的item

一般弹出对话框的内容是个提醒信息,或者是“确认”和“取消”的选择信息,如果要是item是从外部获取或者很多个怎么办,下面就介绍使用ListView来填充Item,并有点击事件! 其实写法非常的简单,...
  • qqHJQS
  • qqHJQS
  • 2015年04月27日 21:49
  • 1560

android图文混排与多种类型listView的混搭的item的demo,getItemViewType()和getViewTypeCount()的使用

工作中经常遇到一个listView里面的item是不同样式的,以前考虑的是在getView实现不同的样式,那样的话效率应该低,也没有那么方便,下面就把最近新学到的东西整理下。就是图文混排的一个list...

ListView中item的最外层使用margin属性失效

参考文章: http://stackoverflow.com/questions/16278159/why-linearlayouts-margin-is-being-ignored-if-used...

android Listview的getView和convertview原理解析以及使用不同的item布局(一)

ListView 和 Adapter 的基础 工作原理: ListView 针对List中每个item,要求 adapter “给我一个视图” (getView)。一个新的视图被返回并显示...

使用布局填充器LayoutInflater 设计ListView的Item样式

// 布局填充器对象, 用于把xml布局转换成view对象 LayoutInflater inflater = MainActivity.this.getLayo...

安卓ListView的Item使用不同的背景颜色

ListView的item显示不同的背景颜色

ListView实现item的卡片效果(不使用RecyclerView+CardView)

现在网上有很多漂亮的listView的显示效果,度娘会告诉你其实很简单不就是用RecyclerView和CardView就实现了嘛,结果代码除了一大串,对于初学者绝对就晕了,技术是死的认识活的,接下来...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:listview中item与label交互使用
举报原因:
原因补充:

(最多只允许输入30个字)