创建可编辑 ListView 控件
ListView 控件是一个在 Windows 应用程序中使用频率比较高的一个,通常使用它来显示数据报表。很多情况下,我们不仅仅使用 ListView 来显示数据,还要求编辑其中的数据。但是 .NET 提供的 ListView 控件的编辑功能十分有限,只能编辑首列,编辑格式只能为文本框,等等,使用起来甚为不便。因此本人新写了一个类,扩充了其功能,客户程序员可以设置任何列的格式 ( 只读,编辑状态下文本框,编辑状态下组合框 ) , 代码如下:
1. ALAN_ListViewColumnStyle 列风格枚举
///
1<summary>
2
3/// 列风格枚举
4
5/// </summary>
public enum ALAN_ListViewColumnStyle
{
ReadOnly, // 只读
EditBox, // 编辑状态下显示为文本框
ComboBox // 编辑状态下显示为组合框
};
2. ALAN_ColumnHeader 带有自定义风格的列
///
1<summary>
2
3/// 列描述
4
5/// </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 列表控件
///
1<summary>
2
3/// 可编辑的 ListView 控件
4
5/// </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
1<this.m_arrcomboboxes.length; !="null)" (columnindex<0="" (m_arrcomboboxes[i]="" alan_listviewcolumnstyle="" color="" columnindex="" columnindex,="" cs)="" editbgcolor="" editfont="" font="" get="" i++)="" if="" m_arrcomboboxes[i].backcolor="this.m_bgcolorComboBox;" public="" return="" set="" setcolumn(int="" this.editbox.backcolor="this.m_bgcolorEdit;" this.editbox.font="this.m_fontEdit;" this.m_bgcoloredit="value;" this.m_bgcoloredit;="" this.m_fontedit="value;" this.m_fontedit;="" void="" {="" ||="" }="">this.Columns.Count)
2
3throw new Exception("Column index is out of range");
4
5((ALAN_ColumnHeader)Columns[columnIndex]).ColumnStyle = cs;
6
7}
8
9public void BoundListToColumn(int columnIndex, string[] items)
10
11{
12
13if (columnIndex<0 || columnIndex>this.Columns.Count)
14
15throw new Exception("Column index is out of range");
16
17if ( ((ALAN_ColumnHeader)Columns[columnIndex]).ColumnStyle != ALAN_ListViewColumnStyle.ComboBox)
18
19throw new Exception("Column should be ComboBox style");
20
21ComboBox newbox = new ComboBox();
22
23for(int i=0; i<items.length; (="" )="" +="new" ,="" cmbbox="(ComboBox)sender;" cmbbox.hide();="" cmbkeypress(object="" cmbselected(object="" combobox="" cr="" e)="" e.keychar="27" esc="" i++)="" if="" int="" newbox.backcolor="this.ComboBoxBgColor;" newbox.dropdownstyle="ComboBoxStyle.DropDownList;" newbox.font="this.ComboBoxFont;" newbox.hide();="" newbox.items.add(items[i]);="" newbox.keypress="" newbox.location="new" newbox.lostfocus="" newbox.selectedindexchanged="" newbox.size="new" or="" press="" private="" sel="" sender="" system.drawing.point(0,0);="" system.drawing.size(0,0);="" system.eventargs="" system.eventhandler(this.cmbfocusover);="" system.eventhandler(this.cmbselected);="" system.windows.forms.control[]="" system.windows.forms.keypresseventargs="" system.windows.forms.keypresseventhandler(this.cmbkeypress);="" this.controls.addrange(new="" this.m_arrcomboboxes[columnindex]="newbox;" void="" {="" {newbox});="" ||="" }="">= 0 )
24
25{
26
27string itemSel = cmbBox.Items[sel].ToString();
28
29m_currentLVItem.SubItems[m_nSubItemSelected].Text = itemSel;
30
31}
32
33}
34
35private void CmbFocusOver(object sender , System.EventArgs e)
36
37{
38
39ComboBox cmbBox = (ComboBox)sender;
40
41cmbBox.Hide() ;
42
43}
44
45private void EditOver(object sender, System.Windows.Forms.KeyPressEventArgs e)
46
47{
48
49if ( e.KeyChar == 13 )
50
51{
52
53m_currentLVItem.SubItems[m_nSubItemSelected].Text = editBox.Text;
54
55editBox.Hide();
56
57}
58
59if ( e.KeyChar == 27 )
60
61editBox.Hide();
62
63}
64
65private void FocusOver(object sender, System.EventArgs e)
66
67{
68
69m_currentLVItem.SubItems[m_nSubItemSelected].Text = editBox.Text;
70
71editBox.Hide();
72
73}
74
75public void SMKDoubleClick(object sender, System.EventArgs e)
76
77{
78
79// Check the subitem clicked .
80
81int nStart = m_nX ; //current mouse down X position
82
83int spos = 0 ;
84
85int epos = this.Columns[0].Width ;
86
87for ( int i=0; i < this.Columns.Count ; i++)
88
89{
90
91if ( nStart > spos && nStart < epos )
92
93{
94
95m_nSubItemSelected = i ;
96
97break;
98
99}
100
101spos = epos ;
102
103epos += this.Columns[i].Width;
104
105}
106
107m_strSubItemText = m_currentLVItem.SubItems[m_nSubItemSelected].Text ;
108
109ALAN_ColumnHeader column = (ALAN_ColumnHeader)Columns[m_nSubItemSelected];
110
111if ( column.ColumnStyle == ALAN_ListViewColumnStyle.ComboBox )
112
113{
114
115ComboBox cmbBox = this.m_arrComboBoxes[m_nSubItemSelected];
116
117if (cmbBox == null)
118
119throw new Exception("The ComboxBox control bind to current column is null");
120
121Rectangle r = new Rectangle(spos , m_currentLVItem.Bounds.Y , epos , m_currentLVItem.Bounds.Bottom);
122
123cmbBox.Size = new System.Drawing.Size(epos - spos , m_currentLVItem.Bounds.Bottom-m_currentLVItem.Bounds.Top);
124
125cmbBox.Location = new System.Drawing.Point(spos , m_currentLVItem.Bounds.Y);
126
127cmbBox.Show() ;
128
129cmbBox.Text = m_strSubItemText;
130
131cmbBox.SelectAll() ;
132
133cmbBox.Focus();
134
135}
136
137if ( column.ColumnStyle == ALAN_ListViewColumnStyle.EditBox )
138
139{
140
141Rectangle r = new Rectangle(spos , m_currentLVItem.Bounds.Y , epos , m_currentLVItem.Bounds.Bottom);
142
143editBox.Size = new System.Drawing.Size(epos - spos , m_currentLVItem.Bounds.Height);
144
145editBox.Location = new System.Drawing.Point(spos , m_currentLVItem.Bounds.Y);
146
147editBox.Show() ;
148
149editBox.Text = m_strSubItemText;
150
151editBox.SelectAll() ;
152
153editBox.Focus();
154
155}
156
157}
158
159public void SMKMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
160
161{
162
163m_currentLVItem = this.GetItemAt(e.X , e.Y);
164
165m_nX = e.X ;
166
167m_nY = e.Y ;
168
169}
170
171}
172
173}
174
175使用方法如下:
176
177this.listView1 = new ALAN</items.length;></this.m_arrcomboboxes.length;>