.Net 创建可编辑ListView控件 :: 程序串串烧

.Net 创建可编辑ListView控件 时间:2004-06-16 hcfalan [ 原作 ] 载自 CSDN http://dev.csdn.net/Develop/article/21/21801.shtm ListView 控件是一个在 Windows 应用程序中使用频率比较高的一个,通常使用它来显示数据报表。很多情况下,我们不仅仅使用 ListView 来显示数据,还要求编辑其中的数据。但是 .NET 提供的 ListView 控件的编辑功能十分有限,只能编辑首列,编辑格式只能为文本框,等等,使用起来甚为不便。因此本人新写了一个类,扩充了其功能,客户程序员可以设置任何列的格式 ( 只读,编辑状态下文本框,编辑状态下组合框 ) , 代码如下: 1. ALAN_ListViewColumnStyle 列风格枚举 /// <summary> /// 列风格枚举 /// </summary> public enum ALAN_ListViewColumnStyle { ReadOnly, // 只读 EditBox, // 编辑状态下显示为文本框 ComboBox // 编辑状态下显示为组合框 }; 2. ALAN_ColumnHeader 带有自定义风格的列 /// <summary> /// 列描述 /// </summary> public class ALAN_ColumnHeader : ColumnHeader { private ALAN_ListViewColumnStyle cs; // 本列的风格 public ALAN_ColumnHeader() : base() { cs = ALAN_ListViewColumnStyle.ReadOnly; } public ALAN_ColumnHeader(ALAN_ListViewColumnStyle _cs) { cs = _cs; } public ALAN_ListViewColumnStyle ColumnStyle { get { return cs; } set { cs = value;} } } 3. ALAN_EditListView 列表控件 /// <summary> /// 可编辑的 ListView 控件 /// </summary> public class ALAN_EditListView : 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 ALAN_EditListView() { 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, ALAN_ListViewColumnStyle cs) { if (columnIndex<0 || columnIndex>this.Columns.Count) throw new Exception("Column index is out of range"); ((ALAN_ColumnHeader)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 ( ((ALAN_ColumnHeader)Columns[columnIndex]).ColumnStyle != ALAN_ListViewColumnStyle.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; if ( e.KeyChar == 13 || e.KeyChar == 27 ) //CR or ESC press { 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 ; ALAN_ColumnHeader column = (ALAN_ColumnHeader)Columns[m_nSubItemSelected]; if ( column.ColumnStyle == ALAN_ListViewColumnStyle.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 == ALAN_ListViewColumnStyle.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 ; } } } 使用方法如下: this.listView1 = new ALAN_EditListView(); /// //////////////////// /// <SPAN lang=EN-US s yizdream 引用Trackback(0) | 编辑 评论 发表评论 用户名: Email: 主页: 最后更新 编程规范 To m c a t和I I S的配合 To m c a t和A p a c h e的配合 播放音乐的脚本 MySQL数据的导出和导入工具:mysqldump 使用ArrayList让String[]数组不固定 embed和bgsound的某些用法 .Net 创建可编辑ListView控件 解决java/jsp乱码的一种转换方法 让tomcat一运行就开始调用某个java类

原始快照:web.archive.org