vb2005 listview的subitem 编辑问题~~~~~~~
我用VB.NET2005做一个小程序,用到listview控件来显示和编辑数据,但是发现一个问题,就是它的subitem项里不能在运行时直接用鼠标在里面编辑数据~~~~~...
我用VB.NET 2005做一个小程序,用到listview控件来显示和编辑数据,但是发现一个问题,就是它的subitem项里不能在运行时直接用鼠标在里面编辑数据~~~~~~~~~~~~~我在网上找到了一个说是能用textbox模拟的,但是那个是用VB6.0实现的,在2005上不能用的,我自己也自己设想用textbox里模拟,但是发现textbox的大小不能设成里面格子的大小,而且textbox的position和size不知道怎么动态的设置~~~~~~~望有实现了的或者是用别的方法或控件能实现数据列表显示并能在界面里能编辑格子里数据的~~~~~能给出小弟帮助~~~~~~~
成功后给加分150~~~~~~~~~ 展开
成功后给加分150~~~~~~~~~ 展开
1个回答
展开全部
DataGridView控件就能直接编辑编辑数据啊,不过不知道是否符合你的需求
给一个简单的双击可编辑ListView的代码供你参考(我做了一点点修改,可以根据你自己的需要再修改):
ListViewEx.cs类文件,继承自ListView
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WinBaidu
{
//msg=0x115 (WM_VSCROLL)
//msg=0x114 (WM_HSCROLL)
/// <summary>
/// CListView 的摘要说明。
/// </summary>
public class ListViewEx : ListView
{
private TextBox m_tb;
public ListViewEx()
{
m_tb = new TextBox();
m_tb.Multiline = true;
m_tb.Visible = false;
this.GridLines = true;
//this.CheckBoxes = false;
this.FullRowSelect = true;
this.Controls.Add(m_tb);
}
private void EditItem(ListViewItem.ListViewSubItem subItem)
{
EditItem(subItem, subItem.Bounds);
}
private void EditItem(ListViewItem.ListViewSubItem subItem, Rectangle rt)
{
if (this.SelectedItems.Count <= 0)
{
return;
}
Rectangle _rect = rt;
m_tb.Bounds = _rect;
m_tb.BringToFront();
m_tb.Font = subItem.Font;
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();
}
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;
base.OnSelectedIndexChanged(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));
}
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;
}
}
}
}
Form1.cs文件
using System;
using System.Windows.Forms;
namespace WinBaidu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AddData();
}
public void AddData()
{
listViewEx1.View = View.Details;
this.listViewEx1.Columns.Add("姓名", 60, HorizontalAlignment.Left);
this.listViewEx1.Columns.Add("性别", 60, HorizontalAlignment.Left);
this.listViewEx1.Columns.Add("年龄", 60, HorizontalAlignment.Left);
this.listViewEx1.Columns.Add("公司", 60, HorizontalAlignment.Left);
this.listViewEx1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "21", "GANA" }));
this.listViewEx1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "22", "GANA" }));
this.listViewEx1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "23", "GANA" }));
this.listViewEx1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "24", "GANA" }));
}
}
}
Form1.Designer.cs文件
namespace WinBaidu
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listViewEx1 = new WinBaidu.ListViewEx();
this.SuspendLayout();
//
// listViewEx1
//
this.listViewEx1.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.listViewEx1.FullRowSelect = true;
this.listViewEx1.GridLines = true;
this.listViewEx1.Location = new System.Drawing.Point(13, 26);
this.listViewEx1.Name = "listViewEx1";
this.listViewEx1.Size = new System.Drawing.Size(549, 358);
this.listViewEx1.TabIndex = 0;
this.listViewEx1.UseCompatibleStateImageBehavior = false;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(574, 405);
this.Controls.Add(this.listViewEx1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private ListViewEx listViewEx1;
}
}
给一个简单的双击可编辑ListView的代码供你参考(我做了一点点修改,可以根据你自己的需要再修改):
ListViewEx.cs类文件,继承自ListView
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WinBaidu
{
//msg=0x115 (WM_VSCROLL)
//msg=0x114 (WM_HSCROLL)
/// <summary>
/// CListView 的摘要说明。
/// </summary>
public class ListViewEx : ListView
{
private TextBox m_tb;
public ListViewEx()
{
m_tb = new TextBox();
m_tb.Multiline = true;
m_tb.Visible = false;
this.GridLines = true;
//this.CheckBoxes = false;
this.FullRowSelect = true;
this.Controls.Add(m_tb);
}
private void EditItem(ListViewItem.ListViewSubItem subItem)
{
EditItem(subItem, subItem.Bounds);
}
private void EditItem(ListViewItem.ListViewSubItem subItem, Rectangle rt)
{
if (this.SelectedItems.Count <= 0)
{
return;
}
Rectangle _rect = rt;
m_tb.Bounds = _rect;
m_tb.BringToFront();
m_tb.Font = subItem.Font;
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();
}
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;
base.OnSelectedIndexChanged(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));
}
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;
}
}
}
}
Form1.cs文件
using System;
using System.Windows.Forms;
namespace WinBaidu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AddData();
}
public void AddData()
{
listViewEx1.View = View.Details;
this.listViewEx1.Columns.Add("姓名", 60, HorizontalAlignment.Left);
this.listViewEx1.Columns.Add("性别", 60, HorizontalAlignment.Left);
this.listViewEx1.Columns.Add("年龄", 60, HorizontalAlignment.Left);
this.listViewEx1.Columns.Add("公司", 60, HorizontalAlignment.Left);
this.listViewEx1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "21", "GANA" }));
this.listViewEx1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "22", "GANA" }));
this.listViewEx1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "23", "GANA" }));
this.listViewEx1.Items.Add(new ListViewItem(new String[] { "李雷", "男", "24", "GANA" }));
}
}
}
Form1.Designer.cs文件
namespace WinBaidu
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listViewEx1 = new WinBaidu.ListViewEx();
this.SuspendLayout();
//
// listViewEx1
//
this.listViewEx1.Font = new System.Drawing.Font("PMingLiU", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136)));
this.listViewEx1.FullRowSelect = true;
this.listViewEx1.GridLines = true;
this.listViewEx1.Location = new System.Drawing.Point(13, 26);
this.listViewEx1.Name = "listViewEx1";
this.listViewEx1.Size = new System.Drawing.Size(549, 358);
this.listViewEx1.TabIndex = 0;
this.listViewEx1.UseCompatibleStateImageBehavior = false;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(574, 405);
this.Controls.Add(this.listViewEx1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private ListViewEx listViewEx1;
}
}
参考资料: http://hi.baidu.com/%B0%D7%D4%C6%D2%BB%C2%C6/blog/item/da42bac3e3c02d5db219a80a.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询