ListView控件中列添加控件ComboBox,控件TextBox,添加時間選擇列DateTimePicker:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;
namespace ListViewControl
{
//msg=0x115 (WM_VSCROLL)橫¨¢向¨°滾?動¡¥
//msg=0x114 (WM_HSCROLL)豎º¨²向¨°滾?動¡¥
/// <summary>
/// CListView 的Ì?摘a要°a說¦Ì明¡Â。¡ê
/// Right Copy Belongs to 魯3一°?鋒¤?
/// QQ:êo379446670
/// </summary>
public class CListView : ListView
{
#region 構1造¨¬函¡¥數ºy
public CListView()
{
this.GridLines = true;
this.CheckBoxes = true;
this.FullRowSelect = true;
this.LabelEdit = true;
m_ctrls = new SortedList<int,object>();
}
#endregion
#region 變À?量¢?
/// <summary>
/// 當Ì¡À前¡ã顯?示º?列¢D表À¨ª
/// </summary>
private Control currentCtrl = null;
/// <summary>
/// 存ä?儲ä¡é需¨¨要°a顯?示º?的Ì?控?件t列¢D表À¨ª
/// </summary>
private SortedList<int, object> m_ctrls = null;
/// <summary>
/// 全¨?局?被À?選?中D的Ì?列¢D索¡Â引°y
/// </summary>
int ColumnIndex = 99999;
#endregion
#region 方¤?法¤¡§
/// <summary>
/// 添¬¨ª加¨®需¨¨要°a顯?示º?的Ì?控?件t
/// </summary>
/// <param name="ctr">顯?示º?的Ì?控?件t(目?前¡ã只?支¡ì持?textbox和¨ªcombox)</param>
/// <param name="columnIndex">在¨²listview中D的Ì?列¢D索¡Â引°y</param>
public void AddDisControl(Control ctr, int columnIndex)
{
if (m_ctrls.ContainsKey(columnIndex))
{
MessageBox.Show("已°?經-包㨹含?所¨´選?的Ì?" + ctr.ToString());
return;
}
ctr.Visible = false;
m_ctrls.Add(columnIndex,(object)ctr);
this.Controls.Add(ctr);
}
private void EditItem(int Index, ListViewItem.ListViewSubItem sb)
{
if (this.SelectedItems.Count <= 0)
{
return;
}
int currentSelectColumnIndex = m_ctrls.IndexOfKey(Index);
if (currentSelectColumnIndex == -1)
{
return;
}
currentCtrl = (Control)m_ctrls.Values[currentSelectColumnIndex];
ListViewItem item = this.SelectedItems[0];
Rectangle rect = sb.Bounds;
Rectangle _rect = new Rectangle(rect.Right - this.Columns[Index].Width, rect.Top, this.Columns[Index].Width, rect.Height);
currentCtrl.Bounds = _rect;
currentCtrl.BringToFront();
currentCtrl.Text = item.SubItems[Index].Text;
currentCtrl.TextChanged += new EventHandler(ctr_TextChanged);
currentCtrl.Leave += new EventHandler(ctr_Leave);
currentCtrl.Visible = true;
currentCtrl.Tag = item;
currentCtrl.Select();
}
#endregion
#region 重?載?事º?件t
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.F2)
{
Point tmpPoint = this.PointToClient(Cursor.Position);
ListViewItem item = this.GetItemAt(tmpPoint.X, tmpPoint.Y);
if (item != null)
{
ListViewItem.ListViewSubItem sb = item.GetSubItemAt(tmpPoint.X, tmpPoint.Y);
ColumnIndex = item.SubItems.IndexOf(sb);
if (tmpPoint.X > this.Columns[0].Width && tmpPoint.X < this.Width)
{
EditItem(ColumnIndex, sb);
}
}
}
base.OnKeyDown(e);
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
if (this.currentCtrl != null)
{
this.currentCtrl.Visible = false;
}
base.OnSelectedIndexChanged(e);
}
protected override void OnDoubleClick(EventArgs e)
{
if (this.SelectedItems.Count == 0)
{
return;
}
Point tmpPoint = this.PointToClient(Cursor.Position);
ListViewItem item = this.GetItemAt(tmpPoint.X, tmpPoint.Y);
if (this.SelectedItems.Count != 0 && item == null)
{
item = this.SelectedItems[0];
}
if (item != null)
{
ListViewItem.ListViewSubItem sb = item.GetSubItemAt(tmpPoint.X, tmpPoint.Y);
ColumnIndex = item.SubItems.IndexOf(sb);
if (tmpPoint.X > this.Columns[0].Width && tmpPoint.X < this.Width)
{
EditItem(ColumnIndex, sb);
}
}
base.OnDoubleClick(e);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x115 )
{
if (currentCtrl != null)
{
currentCtrl.Visible = false;
}
}
if ( m.Msg == 0x114)
{
if (currentCtrl != null)
{
currentCtrl.Visible = false;
}
}
base.WndProc(ref m);
}
#endregion
#region 控?件t事º?件t
private void ctr_Leave(object sender, EventArgs e)
{
if (sender is Control)
{
((Control)sender).TextChanged -= new EventHandler(ctr_TextChanged);
(sender as Control).Visible = false;
}
}
private void ctr_TextChanged(object sender, EventArgs e)
{
if ((sender as Control).Tag is ListViewItem)
{
(((Control)sender).Tag as ListViewItem).SubItems[ColumnIndex].Text = ((Control)sender).Text;
}
}
#endregion
}
}