using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
ListViewEx cListView1 = new ListViewEx();
public Form1()
{
InitializeComponent();
this.Controls.Add(cListView1);
AddData();
DataSet dataset = new DataSet();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//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.Controls.Add(m_tb);//把当前的textbox加入到当前容器里
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;
}
}
}
public void AddData()
{
this.cListView1.GridLines = true;//显示网格线
this.cListView1.FullRowSelect = true;//单击某项的时候选择多行
this.cListView1.View = View.Details;//显示格式;
this.cListView1.Scrollable = true;//添加滚动条
this.cListView1.MultiSelect = false;//不允许选择多行;
this.cListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
this.cListView1.Dock = DockStyle.Fill;
this.cListView1.Columns.Add("姓名", 60, HorizontalAlignment.Left);
this.cListView1.Columns.Add("性别", 60, HorizontalAlignment.Left);
this.cListView1.Columns.Add("年龄", 60, HorizontalAlignment.Left);
this.cListView1.Columns.Add("公司", 60, HorizontalAlignment.Left);
this.cListView1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "23", "GANA" }));
this.cListView1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "23", "GANA" }));
this.cListView1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "23", "GANA" }));
this.cListView1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "23", "GANA" }));
}
}
}
//这是通过自己修改了一点,,,源地址http://hi.baidu.com/%B0%D7%D4%C6%D2%BB%C2%C6/blog/item/da42bac3e3c02d5db219a80a.html