您的位置:首页 > 编程语言 > C#

C#-ListView控件中列添加控件ComboBox,控件TextBox,添加时间选择列DateTimePicker

2013-05-23 23:39 886 查看
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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: