区块链系统之家

关注最新技术动态

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

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">¨²listviewDÌ?¢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

}

}

posted on 2013-05-23 23:39  新技术动态  阅读(4254)  评论(0编辑  收藏  举报

导航