C# WinForm控件ListBox点选可编辑(附源码)

源码下载:http://dotnet.5d6d.com/thread-471-1-1.html
正常情况下,.Net自带的ListBox是无法进行编辑的,欲让它可编辑,我们可添加一个隐藏的文本编辑框,当双击项的时候显示编辑框,更新项内容后隐藏编辑框即可。
欲调整ListBox每个项的高度,ListBox有一个自带属性ItemHeight,于是我们很自然的就设这个属性值,结果却发现改不动,改后又返回默认值12,后面才发现要使该属性值有效更改,必须将DrawMode属性设为OwnerDrawFixed,并且为ListBox添加DrawItem事件:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    e.DrawFocusRectangle();
    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, 
new SolidBrush(e.ForeColor), e.Bounds);
}
经过这样的控制后,设计界面中的ListBox项看不到了,但没关系,运行的时候是可见的,而且每个项的高度确实作了有效更改。
接下来我们为ListBox再添几个事件来控制编辑框的显示,其实就两个:DoubleClick和MouseClick。好了,在添加之前我们在全局区定义一个编辑框txtEdit,并在构造函数为其添加KeyDown事件:
全部代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EditListBox
{
    
public partial class Form1 : Form
    {
        TextBox txtEdit 
= new TextBox();
        
/// <summary>
        
/// KeyDown事件定义
        
/// </summary>
        private void txtEdit_KeyDown(object sender, KeyEventArgs e)
        {
            
//Enter键 更新项并隐藏编辑框
            if (e.KeyCode == Keys.Enter)
            {
                listBox1.Items[listBox1.SelectedIndex] 
= txtEdit.Text;
                txtEdit.Visible 
= false;
            }
            
//Esc键 直接隐藏编辑框
            if (e.KeyCode == Keys.Escape)
                txtEdit.Visible 
= false;
        }
        
public Form1()
        {
            InitializeComponent();
            txtEdit.KeyDown 
+= new KeyEventHandler(txtEdit_KeyDown);
            SetListBox();
            BindListBox();
        }
        
//设置ListBox,高度为20
        private void SetListBox()
        {
            listBox1.DrawMode 
= DrawMode.OwnerDrawFixed;
            listBox1.ItemHeight 
= 20;
        }
        
//绑定ListBox
        private void BindListBox()
        {
            ListItem li 
= new ListItem("0""选项一");
            listBox1.Items.Add(li);
            li 
= new ListItem("1""选项二");
            listBox1.Items.Add(li);
            li 
= new ListItem("2""选项三");
            listBox1.Items.Add(li);
        }
        
/// <summary>
        
/// 选择项类,用于ComboBox或者ListBox添加项
        
/// </summary>
        public class ListItem
        {
            
private string id = string.Empty;
            
private string name = string.Empty;
            
public ListItem(string sid, string sname)
            {
                id 
= sid;
                name 
= sname;
            }
            
public override string ToString()
            {
                
return this.name;
            }
            
public string ID
            {
                
get
                {
                    
return this.id;
                }
                
set
                {
                    
this.id = value;
                }
            }
            
public string Name
            {
                
get
                {
                    
return this.name;
                }
                
set
                {
                    
this.name = value;
                }
            }
        }

        
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, 
new SolidBrush(e.ForeColor), e.Bounds);
        }

        
private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {
            txtEdit.Visible 
= false;
        }

        
private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            
int itemSelected = listBox1.SelectedIndex;
            
string itemText = listBox1.Items[itemSelected].ToString();

            Rectangle rect 
= listBox1.GetItemRectangle(itemSelected);
            txtEdit.Parent 
= listBox1;
            txtEdit.Bounds 
= rect;
            txtEdit.Multiline 
= true;
            txtEdit.Visible 
= true;
            txtEdit.Text 
= itemText;
            txtEdit.Focus();
            txtEdit.SelectAll();
        }
    }
}
经过这样的处理后,我们看看最终的效果:

源码下载:http://dotnet.5d6d.com/thread-471-1-1.html
posted @ 2011-07-14 11:44  大气象  阅读(15050)  评论(1编辑  收藏  举报
http://www.tianqiweiqi.com