土拨鼠的博客
欢迎您!

1.myEditListView类

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

namespace datamanager2
{
    /// <summary>
    /// 可编辑的ListView控件
    /// </summary>
    public class myEditListView : ListView
    {

        private ListViewItem m_currentLVItem;

        private int m_nX = 0;

        private int m_nY = 0;

        private string m_strSubItemText;

        private int m_nSubItemSelected = 0;

        private ComboBox[] m_arrComboBoxes = new ComboBox[20];

        private System.Windows.Forms.TextBox editBox;

        private Font m_fontComboBox;

        private Font m_fontEdit;

        private Color m_bgcolorComboBox;

        private Color m_bgcolorEdit;

 

        public myEditListView()
        {

            editBox = new System.Windows.Forms.TextBox();

            this.ComboBoxFont = this.Font;

            this.EditFont = this.Font;

 

            this.EditBgColor = Color.LightBlue;

            this.m_bgcolorComboBox = Color.LightBlue;

            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SMKMouseDown);

            this.DoubleClick += new System.EventHandler(this.SMKDoubleClick);

            this.GridLines = true;

 

            editBox.Size = new System.Drawing.Size(0, 0);

            editBox.Location = new System.Drawing.Point(0, 0);

            this.Controls.AddRange(new System.Windows.Forms.Control[] { this.editBox });

            editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);

            editBox.LostFocus += new System.EventHandler(this.FocusOver);

            editBox.AutoSize = true;

            editBox.Font = this.EditFont;

            editBox.BackColor = this.EditBgColor;

            editBox.BorderStyle = BorderStyle.FixedSingle;

            editBox.Hide();

            editBox.Text = "";

        }

 

        public Font ComboBoxFont
        {

            get { return this.m_fontComboBox; }

            set { this.m_fontComboBox = value; }

        }

 

        public Color ComboBoxBgColor
        {

            get { return this.m_bgcolorComboBox; }

            set
            {

                this.m_bgcolorComboBox = value;

                for (int i = 0; i < this.m_arrComboBoxes.Length; i++)
                {

                    if (m_arrComboBoxes[i] != null)

                        m_arrComboBoxes[i].BackColor = this.m_bgcolorComboBox;

                }

            }

        }

 

        public Font EditFont
        {

            get { return this.m_fontEdit; }

            set
            {

                this.m_fontEdit = value;

                this.editBox.Font = this.m_fontEdit;

            }

        }

 

        public Color EditBgColor
        {

            get { return this.m_bgcolorEdit; }

            set
            {

                this.m_bgcolorEdit = value;

                this.editBox.BackColor = this.m_bgcolorEdit;

            }

        }

 

        public void SetColumn(int columnIndex, myListViewColumnStyle cs)
        {

            if (columnIndex < 0 || columnIndex > this.Columns.Count)

                throw new Exception("Column index is out of range");

            ((myColumnHeader)Columns[columnIndex]).ColumnStyle = cs;

        }

 

        public void BoundListToColumn(int columnIndex, string[] items)
        {

            if (columnIndex < 0 || columnIndex > this.Columns.Count)

                throw new Exception("Column index is out of range");

            if (((myColumnHeader)Columns[columnIndex]).ColumnStyle != myListViewColumnStyle.ComboBox)

                throw new Exception("Column should be ComboBox style");

 

            ComboBox newbox = new ComboBox();

            for (int i = 0; i < items.Length; i++)

                newbox.Items.Add(items[i]);

            newbox.Size = new System.Drawing.Size(0, 0);

            newbox.Location = new System.Drawing.Point(0, 0);

            this.Controls.AddRange(new System.Windows.Forms.Control[] { newbox });

            newbox.SelectedIndexChanged += new System.EventHandler(this.CmbSelected);

            newbox.LostFocus += new System.EventHandler(this.CmbFocusOver);

            newbox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CmbKeyPress);

            newbox.Font = this.ComboBoxFont;

            newbox.BackColor = this.ComboBoxBgColor;

            newbox.DropDownStyle = ComboBoxStyle.DropDownList;

            newbox.Hide();

            this.m_arrComboBoxes[columnIndex] = newbox;

        }

 

        private void CmbKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {

            ComboBox cmbBox = (ComboBox)sender;
            //CR or ESC press
            if (e.KeyChar == 13 || e.KeyChar == 27)
            {

                cmbBox.Hide();

            }

        }

 

        private void CmbSelected(object sender, System.EventArgs e)
        {

            ComboBox cmbBox = (ComboBox)sender;

            int sel = cmbBox.SelectedIndex;

            if (sel >= 0)
            {

                string itemSel = cmbBox.Items[sel].ToString();

                m_currentLVItem.SubItems[m_nSubItemSelected].Text = itemSel;

            }

        }

 

        private void CmbFocusOver(object sender, System.EventArgs e)
        {

            ComboBox cmbBox = (ComboBox)sender;

            cmbBox.Hide();

        }

 

        private void EditOver(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {

            if (e.KeyChar == 13)
            {

                m_currentLVItem.SubItems[m_nSubItemSelected].Text = editBox.Text;

                editBox.Hide();

            }

 

            if (e.KeyChar == 27)

                editBox.Hide();

        }

 

        private void FocusOver(object sender, System.EventArgs e)
        {

            m_currentLVItem.SubItems[m_nSubItemSelected].Text = editBox.Text;

            editBox.Hide();

        }

 

        public void SMKDoubleClick(object sender, System.EventArgs e)
        {

            // Check the subitem clicked .

            int nStart = m_nX; //current mouse down X position

            int spos = 0;

            int epos = this.Columns[0].Width;

            for (int i = 0; i < this.Columns.Count; i++)
            {

                if (nStart > spos && nStart < epos)
                {

                    m_nSubItemSelected = i;

                    break;

                }

 

                spos = epos;

                epos += this.Columns[i].Width;

            }

 

            m_strSubItemText = m_currentLVItem.SubItems[m_nSubItemSelected].Text;

 

            myColumnHeader column = (myColumnHeader)Columns[m_nSubItemSelected];

            if (column.ColumnStyle == myListViewColumnStyle.ComboBox)
            {

                ComboBox cmbBox = this.m_arrComboBoxes[m_nSubItemSelected];

                if (cmbBox == null)

                    throw new Exception("The ComboxBox control bind to current column is null");

                Rectangle r = new Rectangle(spos, m_currentLVItem.Bounds.Y, epos, m_currentLVItem.Bounds.Bottom);

                cmbBox.Size = new System.Drawing.Size(epos - spos, m_currentLVItem.Bounds.Bottom - m_currentLVItem.Bounds.Top);

                cmbBox.Location = new System.Drawing.Point(spos, m_currentLVItem.Bounds.Y);

                cmbBox.Show();

                cmbBox.Text = m_strSubItemText;

                cmbBox.SelectAll();

                cmbBox.Focus();

            }

            if (column.ColumnStyle == myListViewColumnStyle.EditBox)
            {

                Rectangle r = new Rectangle(spos, m_currentLVItem.Bounds.Y, epos, m_currentLVItem.Bounds.Bottom);

                editBox.Size = new System.Drawing.Size(epos - spos, m_currentLVItem.Bounds.Height);

                editBox.Location = new System.Drawing.Point(spos, m_currentLVItem.Bounds.Y);

                editBox.Show();

                editBox.Text = m_strSubItemText;

                editBox.SelectAll();

                editBox.Focus();

            }

        }

 

        public void SMKMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {

            m_currentLVItem = this.GetItemAt(e.X, e.Y);

            m_nX = e.X;

            m_nY = e.Y;

        }
    }
}

2.列风格

 

using System;
using System.Collections.Generic;
using System.Text;

namespace datamanager2
{
    /// <summary>
    /// 列风格枚举
    /// </summary>
    public enum myListViewColumnStyle
    {

        ReadOnly=0, //只读

        EditBox=1,  //编辑状态下显示为文本框

        ComboBox=2  //编辑状态下显示为组合框

    }

}

3列描述

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace datamanager2
{
    /// <summary>
    /// 列描述
    /// </summary>
    public class myColumnHeader : ColumnHeader
    {
        //本列的风格
        private myListViewColumnStyle cs;

        public myColumnHeader()
            : base()
        {
            cs = myListViewColumnStyle.ReadOnly;
        }

        public myColumnHeader(myListViewColumnStyle _cs)
        {
            cs = _cs;
        }

        public myListViewColumnStyle ColumnStyle
        {
            get { return cs; }
            set { cs = value; }
        }
    };
}

 

4先建一个editListView控件,然后将如下代码拷贝到您的窗体_load中,运行您的代码.

private datamanager2.myColumnHeader header1 = new datamanager2.myColumnHeader(datamanager2.myListViewColumnStyle.ReadOnly);
        private datamanager2.myColumnHeader header2 = new datamanager2.myColumnHeader(datamanager2.myListViewColumnStyle.EditBox);
        private datamanager2.myColumnHeader header3 = new datamanager2.myColumnHeader(datamanager2.myListViewColumnStyle.ComboBox);

 

 

this.SuspendLayout();
            //
            // header1
            //
            this.header1 .ColumnStyle  = datamanager2.myListViewColumnStyle.ReadOnly;
            this.header1.Text = "字段名称";
            this.header1.Width = 100;
            //
            // header2
            //
            this.header2.ColumnStyle = datamanager2.myListViewColumnStyle.EditBox ;
            this.header2.Text = "字段类型";
            this.header2.Width = 100;
            //
            // header3
            //
            this.header3.ColumnStyle = datamanager2.myListViewColumnStyle.ComboBox ;
            this.header3.Text = "是否主键";
            this.header3.Width = 110;

            this.editListView.Columns.AddRange(new datamanager2.myColumnHeader [] {
            this.header1,
            this.header2,
            this.header3});
           
            this.editListView .BoundListToColumn(2, new string[] { "是", "否"});//comboBox项
            this.editListView.ComboBoxFont = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

            this.editListView.EditFont = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

           

            this.editListView.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

            this.editListView.FullRowSelect = true;//必须

           //为editListView添加新项

            ListViewItem lvitem = new ListViewItem();
            lvitem.SubItems.Clear();
            lvitem.SubItems [0].Text ="此处添加字段名称";
            lvitem.SubItems .Add ("此处添加类型");
            lvitem .SubItems .Add ("否");
            this.editListView.Items.Add(lvitem);

            this.editListView.View = View.Details ;//必须

            
                      

            this.ResumeLayout(false);

posted on 2008-10-03 09:51  groundhog  阅读(259)  评论(0)    收藏  举报