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
}
}
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
}
}
相关文章推荐
- vs2015 c#通过ComboBox选择查询类别并在TextBox输入条件进行查询
- C# 动态生成textbox,Combobox等控件
- android中ListView添加CheckBox控件实现选择
- vs2015 c#datagridview表将列名添加到comboBox1控件中
- [代码问题解决录A1]c#Listview控件动态添加数据并阻止重复添加
- ASP.NET中时间选择控件的添加
- C# 向 ListView 控件添加搜索功能
- c#(winform)中ComboBox和ListBox添加项,写一个用户控件
- C# listview添加combobox到单元格的实现代码
- C#(WinForm)ComboBox和ListBox添加项及设置默认选择项
- c#中listview控件添加信息例程
- winform中为ComboBox控件添加“请选择”或“全部”选项
- C# 程序中如何向(DEV)ComboBoxEdit控件中添加下拉列表的值不使用数据绑定,就是使用代码添加
- winform中为ComboBox控件添加“请选择”或“全部”选项
- C#(WinForm) ComboBox和ListBox添加项及设置默认选择项
- c#datagridview中添加时间控件DateTimePicker
- My97 datepicker 日期控件添加时间自动选择 年龄,生肖,星座
- C#如何遍历动态生成的textbox,Combobox 等控件
- ASP.net 页面添加时间选择控件
- C# comboBox控件使用之添加读取项用法