C# 如何将ListView的单元格双击之后可以编辑

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 如何将ListView的单元格双击之后可以编辑相关的知识,希望对你有一定的参考价值。

参考技术A using System;
using System.Drawing;
using System.Windows.Forms;
namespace WinFormListviewAddContrl

//msg=0x115 (WM_VSCROLL)
//msg=0x114 (WM_HSCROLL)
/// <summary>
/// CListView 的摘要说明。
/// </summary>
public class ListViewEx : ListView

private TextBox m_tb;
private ComboBox m_cb;
public ListViewEx()

m_tb = new TextBox();
m_cb = new ComboBox();
m_tb.Multiline = true;
m_tb.Visible = false;
m_cb.Visible = false;
this.GridLines = true;
//this.CheckBoxes = false;
this.FullRowSelect = true;
this.Controls.Add(m_tb);
this.Controls.Add(m_cb);

private void EditItem(ListViewItem.ListViewSubItem subItem)

if (this.SelectedItems.Count <= 0)

return;


Rectangle _rect = subItem.Bounds;
m_tb.Bounds = _rect;
m_tb.BringToFront();
m_tb.Text = subItem.Text;
m_tb.Leave += new EventHandler(tb_Leave);
m_tb.TextChanged += new EventHandler(m_tb_TextChanged);
m_tb.Visible = true;
m_tb.Tag = subItem;
m_tb.Select();

private void EditItem(ListViewItem.ListViewSubItem subItem, Rectangle rt)

if (this.SelectedItems.Count <= 0)

return;

Rectangle _rect = rt;
m_cb.Bounds = _rect;
m_cb.BringToFront();
m_cb.Items.Add(subItem.Text);
m_cb.Text = subItem.Text;
m_cb.Leave += new EventHandler(lstb_Leave);
m_cb.TextChanged += new EventHandler(m_lstb_TextChanged);
m_cb.Visible = true;
m_cb.Tag = subItem;
m_cb.Select();


protected override void OnKeyDown(KeyEventArgs e)

if (e.KeyCode == Keys.F2)

if (this.SelectedItems.Count > 0)

//this.SelectedItems[0].BeginEdit();
ListViewItem lvi = this.SelectedItems[0];
EditItem(lvi.SubItems[0],new Rectangle(lvi.Bounds.Left,lvi.Bounds.Top,this.Columns[0].Width,lvi.Bounds.Height-2));


base.OnKeyDown(e);

protected override void OnSelectedIndexChanged(EventArgs e)

this.m_tb.Visible = false;
this.m_cb.Visible = false;
base.OnSelectedIndexChanged(e);

//protected override void OnDoubleClick(EventArgs e)
//
// Point tmpPoint = this.PointToClient(Cursor.Position);
// ListViewItem item = this.GetItemAt(tmpPoint.X, tmpPoint.Y);
// if (item != null)
//
// if (tmpPoint.X > this.Columns[0].Width && tmpPoint.X < this.Width)
//
// EditItem(1);
//
//
// base.OnDoubleClick(e);
//
protected override void OnDoubleClick(EventArgs e)

Point tmpPoint = this.PointToClient(Cursor.Position);
ListViewItem.ListViewSubItem subitem = this.HitTest(tmpPoint).SubItem;
ListViewItem item = this.HitTest(tmpPoint).Item;
if (subitem != null)

if (item.SubItems[0].Equals(subitem))

EditItem(subitem, new Rectangle(item.Bounds.Left, item.Bounds.Top, this.Columns[0].Width, item.Bounds.Height-2));

else

EditItem(subitem);


base.OnDoubleClick(e);

protected override void WndProc(ref Message m)

if (m.Msg == 0x115 || m.Msg == 0x114)

this.m_tb.Visible = false;

base.WndProc(ref m);

private void tb_Leave(object sender, EventArgs e)

m_tb.TextChanged -= new EventHandler(m_tb_TextChanged);
(sender as TextBox).Visible = false;

private void m_tb_TextChanged(object sender, EventArgs e)

if ((sender as TextBox).Tag is ListViewItem.ListViewSubItem)

(this.m_tb.Tag as ListViewItem.ListViewSubItem).Text = this.m_tb.Text;



private void lstb_Leave(object sender, EventArgs e)

m_cb.TextChanged -= new EventHandler(m_lstb_TextChanged);

private void m_lstb_TextChanged(object sender, EventArgs e)

if ((sender as ListBox).Tag is ListViewItem.ListViewSubItem)

(this.m_cb.Tag as ListViewItem.ListViewSubItem).Text = this.m_cb.Text;



以上是关于C# 如何将ListView的单元格双击之后可以编辑的主要内容,如果未能解决你的问题,请参考以下文章

我如何在WPF DataGrid上处理单元格双击事件,相当于Windows DataGrid的事件?

EXCEL单元格双击一下就自动换行,我已把自动换行取消了。为啥双击一下单元格还是会自动换行。

excel双击单元格无法进行编辑,只能在公式栏输入,怎么办?

C++ Qt 中的tableWidget单元格内容双击 后在修改后的内容 怎么获得啊

c# 如何双击listview 的项可变成编辑状态?

C# winform 的listView控件如何将单元格写入值