如何把TextBox 和 combox用到Listview里去,在线急等~~~

dudulang 2006-05-31 10:15:00
在做一个项目,需要用到Listview,当鼠标双击时在改子项的位置出现文本框,用做编辑用,或者出现Combox,请问如何实现,在线等,急盼~~~谢谢~~~
最好有源码.
...全文
429 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
abblly 2006-07-17
  • 打赏
  • 举报
回复
http://www.microsoft.com/china/MSDN/library/archives/library/dndotnet/html/usingpropgrid.asp
abblly 2006-07-17
  • 打赏
  • 举报
回复
建议用propertyGrid,.netFramework自带的。
LGame 2006-06-01
  • 打赏
  • 举报
回复
楼上的好强啊,,

当时我是做了个假的,,

移到哪条记录就显来个COMBOX
代码蜗牛sky 2006-06-01
  • 打赏
  • 举报
回复
1.ALAN_ListViewColumnStyle 列风格枚举
/// <summary>
/// 列风格枚举
/// </summary>
public enum ALAN_ListViewColumnStyle
{
ReadOnly, //只读
EditBox, //编辑状态下显示为文本框
ComboBox //编辑状态下显示为组合框
};

2.ALAN_ColumnHeader 带有自定义风格的列
/// <summary>
/// 列描述
/// </summary>
public class ALAN_ColumnHeader : ColumnHeader
{
private ALAN_ListViewColumnStyle cs; //本列的风格
public ALAN_ColumnHeader() : base()
{
cs = ALAN_ListViewColumnStyle.ReadOnly;
}
public ALAN_ColumnHeader(ALAN_ListViewColumnStyle _cs)
{
cs = _cs;
}
public ALAN_ListViewColumnStyle ColumnStyle
{
get { return cs; }
set { cs = value;}
}
}

3.ALAN_EditListView 列表控件

/// <summary>
/// 可编辑的ListView控件
/// </summary>
public class ALAN_EditListView : ListView
{
private ListViewItem m_currentLVItem;
private int m_nX=0;
private int m_nY=0;
private string m_strSubItemText ;
private int m_nSubItemSelected = 0 ;
private ComboBox[] m_arrComboBoxes = new ComboBox[20];
private System.Windows.Forms.TextBox editBox;
private Font m_fontComboBox;
private Font m_fontEdit;
private Color m_bgcolorComboBox;
private Color m_bgcolorEdit;

public ALAN_EditListView()
{
editBox = new System.Windows.Forms.TextBox();
this.ComboBoxFont = this.Font;
this.EditFont = this.Font;

this.EditBgColor = Color.LightBlue;
this.m_bgcolorComboBox = Color.LightBlue;
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SMKMouseDown);
this.DoubleClick += new System.EventHandler(this.SMKDoubleClick);
this.GridLines = true ;

editBox.Size = new System.Drawing.Size(0,0);
editBox.Location = new System.Drawing.Point(0,0);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.editBox});
editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);
editBox.LostFocus += new System.EventHandler(this.FocusOver);
editBox.AutoSize = true;
editBox.Font = this.EditFont;
editBox.BackColor = this.EditBgColor;
editBox.BorderStyle = BorderStyle.FixedSingle;
editBox.Hide();
editBox.Text = "";
}

public Font ComboBoxFont
{
get { return this.m_fontComboBox; }
set { this.m_fontComboBox = value;}
}

public Color ComboBoxBgColor
{
get { return this.m_bgcolorComboBox; }
set
{
this.m_bgcolorComboBox = value;
for(int i=0; i<this.m_arrComboBoxes.Length; i++)
{
if (m_arrComboBoxes[i] != null)
m_arrComboBoxes[i].BackColor = this.m_bgcolorComboBox;
}
}
}

public Font EditFont
{
get { return this.m_fontEdit; }
set
{
this.m_fontEdit = value;
this.editBox.Font = this.m_fontEdit;
}
}

public Color EditBgColor
{
get { return this.m_bgcolorEdit; }
set
{
this.m_bgcolorEdit = value;
this.editBox.BackColor = this.m_bgcolorEdit;
}
}

public void SetColumn(int columnIndex, ALAN_ListViewColumnStyle cs)
{
if (columnIndex<0 || columnIndex>this.Columns.Count)
throw new Exception("Column index is out of range");
((ALAN_ColumnHeader)Columns[columnIndex]).ColumnStyle = cs;
}

public void BoundListToColumn(int columnIndex, string[] items)
{
if (columnIndex<0 || columnIndex>this.Columns.Count)
throw new Exception("Column index is out of range");
if ( ((ALAN_ColumnHeader)Columns[columnIndex]).ColumnStyle != ALAN_ListViewColumnStyle.ComboBox)
throw new Exception("Column should be ComboBox style");

ComboBox newbox = new ComboBox();
for(int i=0; i<items.Length; i++)
newbox.Items.Add(items[i]);
newbox.Size = new System.Drawing.Size(0,0);
newbox.Location = new System.Drawing.Point(0,0);
this.Controls.AddRange(new System.Windows.Forms.Control[] {newbox});
newbox.SelectedIndexChanged += new System.EventHandler(this.CmbSelected);
newbox.LostFocus += new System.EventHandler(this.CmbFocusOver);
newbox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.CmbKeyPress);
newbox.Font = this.ComboBoxFont;
newbox.BackColor = this.ComboBoxBgColor;
newbox.DropDownStyle = ComboBoxStyle.DropDownList;
newbox.Hide();
this.m_arrComboBoxes[columnIndex] = newbox;
}

private void CmbKeyPress(object sender , System.Windows.Forms.KeyPressEventArgs e)
{
ComboBox cmbBox = (ComboBox)sender;
if ( e.KeyChar == 13 || e.KeyChar == 27 ) //CR or ESC press
{
cmbBox.Hide();
}
}

private void CmbSelected(object sender , System.EventArgs e)
{
ComboBox cmbBox = (ComboBox)sender;
int sel = cmbBox.SelectedIndex;
if ( sel >= 0 )
{
string itemSel = cmbBox.Items[sel].ToString();
m_currentLVItem.SubItems[m_nSubItemSelected].Text = itemSel;
}
}

private void CmbFocusOver(object sender , System.EventArgs e)
{
ComboBox cmbBox = (ComboBox)sender;
cmbBox.Hide() ;
}

private void EditOver(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ( e.KeyChar == 13 )
{
m_currentLVItem.SubItems[m_nSubItemSelected].Text = editBox.Text;
editBox.Hide();
}

if ( e.KeyChar == 27 )
editBox.Hide();
}

private void FocusOver(object sender, System.EventArgs e)
{
m_currentLVItem.SubItems[m_nSubItemSelected].Text = editBox.Text;
editBox.Hide();
}

public void SMKDoubleClick(object sender, System.EventArgs e)
{
// Check the subitem clicked .
int nStart = m_nX ; //current mouse down X position
int spos = 0 ;
int epos = this.Columns[0].Width ;
for ( int i=0; i < this.Columns.Count ; i++)
{
if ( nStart > spos && nStart < epos )
{
m_nSubItemSelected = i ;
break;
}

spos = epos ;
epos += this.Columns[i].Width;
}

m_strSubItemText = m_currentLVItem.SubItems[m_nSubItemSelected].Text ;

ALAN_ColumnHeader column = (ALAN_ColumnHeader)Columns[m_nSubItemSelected];
if ( column.ColumnStyle == ALAN_ListViewColumnStyle.ComboBox )
{
ComboBox cmbBox = this.m_arrComboBoxes[m_nSubItemSelected];
if (cmbBox == null)
throw new Exception("The ComboxBox control bind to current column is null");
Rectangle r = new Rectangle(spos , m_currentLVItem.Bounds.Y , epos , m_currentLVItem.Bounds.Bottom);
cmbBox.Size = new System.Drawing.Size(epos - spos , m_currentLVItem.Bounds.Bottom-m_currentLVItem.Bounds.Top);
cmbBox.Location = new System.Drawing.Point(spos , m_currentLVItem.Bounds.Y);
cmbBox.Show() ;
cmbBox.Text = m_strSubItemText;
cmbBox.SelectAll() ;
cmbBox.Focus();
}
if ( column.ColumnStyle == ALAN_ListViewColumnStyle.EditBox )
{
Rectangle r = new Rectangle(spos , m_currentLVItem.Bounds.Y , epos , m_currentLVItem.Bounds.Bottom);
editBox.Size = new System.Drawing.Size(epos - spos , m_currentLVItem.Bounds.Height);
editBox.Location = new System.Drawing.Point(spos , m_currentLVItem.Bounds.Y);
editBox.Show() ;
editBox.Text = m_strSubItemText;
editBox.SelectAll() ;
editBox.Focus();
}
}

public void SMKMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
m_currentLVItem = this.GetItemAt(e.X , e.Y);
m_nX = e.X ;
m_nY = e.Y ;
}
}
}
lovvver 2006-06-01
  • 打赏
  • 举报
回复
楼主抱歉,后半部分多发了一次。
lovvver 2006-06-01
  • 打赏
  • 举报
回复
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
// Look for WM_VSCROLL,WM_HSCROLL or WM_SIZE messages.
case WM_VSCROLL:
case WM_HSCROLL:
case WM_SIZE:
EndEditing(false);
break;
case WM_NOTIFY:
// Look for WM_NOTIFY of events that might also change the
// editor's position/size: Column reordering or resizing
NMHDR h = (NMHDR)Marshal.PtrToStructure(msg.LParam, typeof(NMHDR));
if (h.code == HDN_BEGINDRAG ||
h.code == HDN_ITEMCHANGINGA ||
h.code == HDN_ITEMCHANGINGW)
EndEditing(false);
break;
}

base.WndProc(ref msg);
}


#region Initialize editing depending of DoubleClickActivation property
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseUp(e);

if (DoubleClickActivation)
{
return;
}

EditSubitemAt(new Point(e.X, e.Y));
}

protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick (e);

if (!DoubleClickActivation)
{
return;
}

Point pt = this.PointToClient(Cursor.Position);

EditSubitemAt(pt);
}

///<summary>
/// Fire SubItemClicked
///</summary>
///<param name="p">Point of click/doubleclick</param>
private void EditSubitemAt(Point p)
{
ListViewItem item;
int idx = GetSubItemAt(p.X, p.Y, out item);
if (idx >= 0)
{
OnSubItemClicked(new SubItemEventArgs(item, idx));
}
}

#endregion

#region In-place editing functions
// The control performing the actual editing
private Control _editingControl;
// The LVI being edited
private ListViewItem _editItem;
// The SubItem being edited
private int _editSubItem;

protected void OnSubItemBeginEditing(SubItemEventArgs e)
{
if (SubItemBeginEditing != null)
SubItemBeginEditing(this, e);
}
protected void OnSubItemEndEditing(SubItemEndEditingEventArgs e)
{
if (SubItemEndEditing != null)
SubItemEndEditing(this, e);
}
protected void OnSubItemClicked(SubItemEventArgs e)
{
if (SubItemClicked != null)
SubItemClicked(this, e);
}


/// <summary>
/// Begin in-place editing of given cell
/// </summary>
/// <param name="c">Control used as cell editor</param>
/// <param name="Item">ListViewItem to edit</param>
/// <param name="SubItem">SubItem index to edit</param>
public void StartEditing(Control c, ListViewItem Item, int SubItem)
{
OnSubItemBeginEditing(new SubItemEventArgs(Item, SubItem));

Rectangle rcSubItem = GetSubItemBounds(Item, SubItem);

if (rcSubItem.X < 0)
{
// Left edge of SubItem not visible - adjust rectangle position and width
rcSubItem.Width += rcSubItem.X;
rcSubItem.X=0;
}
if (rcSubItem.X+rcSubItem.Width > this.Width)
{
// Right edge of SubItem not visible - adjust rectangle width
rcSubItem.Width = this.Width-rcSubItem.Left;
}

// Subitem bounds are relative to the location of the ListView!
rcSubItem.Offset(Left, Top);

// In case the editing control and the listview are on different parents,
// account for different origins
Point origin = new Point(0,0);
Point lvOrigin = this.Parent.PointToScreen(origin);
Point ctlOrigin = c.Parent.PointToScreen(origin);

rcSubItem.Offset(lvOrigin.X-ctlOrigin.X, lvOrigin.Y-ctlOrigin.Y);

// Position and show editor
c.Bounds = rcSubItem;
c.Text = Item.SubItems[SubItem].Text;
c.Visible = true;
c.BringToFront();
c.Focus();

_editingControl = c;
_editingControl.Leave += new EventHandler(_editControl_Leave);
_editingControl.KeyPress += new KeyPressEventHandler(_editControl_KeyPress);

_editItem = Item;
_editSubItem = SubItem;
}


private void _editControl_Leave(object sender, EventArgs e)
{
// cell editor losing focus
EndEditing(true);
}

private void _editControl_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)(int)Keys.Escape:
{
EndEditing(false);
break;
}

case (char)(int)Keys.Enter:
{
EndEditing(true);
break;
}
}
}

/// <summary>
/// Accept or discard current value of cell editor control
/// </summary>
/// <param name="AcceptChanges">Use the _editingControl's Text as new SubItem text or discard changes?</param>
public void EndEditing(bool AcceptChanges)
{
if (_editingControl == null)
return;

SubItemEndEditingEventArgs e = new SubItemEndEditingEventArgs(
_editItem, // The item being edited
_editSubItem, // The subitem index being edited
AcceptChanges ?
_editingControl.Text : // Use editControl text if changes are accepted
_editItem.SubItems[_editSubItem].Text, // or the original subitem's text, if changes are discarded
!AcceptChanges // Cancel?
);

OnSubItemEndEditing(e);

_editItem.SubItems[_editSubItem].Text = e.DisplayText;

_editingControl.Leave -= new EventHandler(_editControl_Leave);
_editingControl.KeyPress -= new KeyPressEventHandler(_editControl_KeyPress);

_editingControl.Visible = false;

_editingControl = null;
_editItem = null;
_editSubItem = -1;
}
#endregion
}
}
lovvver 2006-06-01
  • 打赏
  • 举报
回复
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
// Look for WM_VSCROLL,WM_HSCROLL or WM_SIZE messages.
case WM_VSCROLL:
case WM_HSCROLL:
case WM_SIZE:
EndEditing(false);
break;
case WM_NOTIFY:
// Look for WM_NOTIFY of events that might also change the
// editor's position/size: Column reordering or resizing
NMHDR h = (NMHDR)Marshal.PtrToStructure(msg.LParam, typeof(NMHDR));
if (h.code == HDN_BEGINDRAG ||
h.code == HDN_ITEMCHANGINGA ||
h.code == HDN_ITEMCHANGINGW)
EndEditing(false);
break;
}

base.WndProc(ref msg);
}


#region Initialize editing depending of DoubleClickActivation property
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseUp(e);

if (DoubleClickActivation)
{
return;
}

EditSubitemAt(new Point(e.X, e.Y));
}

protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick (e);

if (!DoubleClickActivation)
{
return;
}

Point pt = this.PointToClient(Cursor.Position);

EditSubitemAt(pt);
}

///<summary>
/// Fire SubItemClicked
///</summary>
///<param name="p">Point of click/doubleclick</param>
private void EditSubitemAt(Point p)
{
ListViewItem item;
int idx = GetSubItemAt(p.X, p.Y, out item);
if (idx >= 0)
{
OnSubItemClicked(new SubItemEventArgs(item, idx));
}
}

#endregion

#region In-place editing functions
// The control performing the actual editing
private Control _editingControl;
// The LVI being edited
private ListViewItem _editItem;
// The SubItem being edited
private int _editSubItem;

protected void OnSubItemBeginEditing(SubItemEventArgs e)
{
if (SubItemBeginEditing != null)
SubItemBeginEditing(this, e);
}
protected void OnSubItemEndEditing(SubItemEndEditingEventArgs e)
{
if (SubItemEndEditing != null)
SubItemEndEditing(this, e);
}
protected void OnSubItemClicked(SubItemEventArgs e)
{
if (SubItemClicked != null)
SubItemClicked(this, e);
}


/// <summary>
/// Begin in-place editing of given cell
/// </summary>
/// <param name="c">Control used as cell editor</param>
/// <param name="Item">ListViewItem to edit</param>
/// <param name="SubItem">SubItem index to edit</param>
public void StartEditing(Control c, ListViewItem Item, int SubItem)
{
OnSubItemBeginEditing(new SubItemEventArgs(Item, SubItem));

Rectangle rcSubItem = GetSubItemBounds(Item, SubItem);

if (rcSubItem.X < 0)
{
// Left edge of SubItem not visible - adjust rectangle position and width
rcSubItem.Width += rcSubItem.X;
rcSubItem.X=0;
}
if (rcSubItem.X+rcSubItem.Width > this.Width)
{
// Right edge of SubItem not visible - adjust rectangle width
rcSubItem.Width = this.Width-rcSubItem.Left;
}

// Subitem bounds are relative to the location of the ListView!
rcSubItem.Offset(Left, Top);

// In case the editing control and the listview are on different parents,
// account for different origins
Point origin = new Point(0,0);
Point lvOrigin = this.Parent.PointToScreen(origin);
Point ctlOrigin = c.Parent.PointToScreen(origin);

rcSubItem.Offset(lvOrigin.X-ctlOrigin.X, lvOrigin.Y-ctlOrigin.Y);

// Position and show editor
c.Bounds = rcSubItem;
c.Text = Item.SubItems[SubItem].Text;
c.Visible = true;
c.BringToFront();
c.Focus();

_editingControl = c;
_editingControl.Leave += new EventHandler(_editControl_Leave);
_editingControl.KeyPress += new KeyPressEventHandler(_editControl_KeyPress);

_editItem = Item;
_editSubItem = SubItem;
}


private void _editControl_Leave(object sender, EventArgs e)
{
// cell editor losing focus
EndEditing(true);
}

private void _editControl_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)(int)Keys.Escape:
{
EndEditing(false);
break;
}

case (char)(int)Keys.Enter:
{
EndEditing(true);
break;
}
}
}

/// <summary>
/// Accept or discard current value of cell editor control
/// </summary>
/// <param name="AcceptChanges">Use the _editingControl's Text as new SubItem text or discard changes?</param>
public void EndEditing(bool AcceptChanges)
{
if (_editingControl == null)
return;

SubItemEndEditingEventArgs e = new SubItemEndEditingEventArgs(
_editItem, // The item being edited
_editSubItem, // The subitem index being edited
AcceptChanges ?
_editingControl.Text : // Use editControl text if changes are accepted
_editItem.SubItems[_editSubItem].Text, // or the original subitem's text, if changes are discarded
!AcceptChanges // Cancel?
);

OnSubItemEndEditing(e);

_editItem.SubItems[_editSubItem].Text = e.DisplayText;

_editingControl.Leave -= new EventHandler(_editControl_Leave);
_editingControl.KeyPress -= new KeyPressEventHandler(_editControl_KeyPress);

_editingControl.Visible = false;

_editingControl = null;
_editItem = null;
_editSubItem = -1;
}
#endregion
}
}
lovvver 2006-06-01
  • 打赏
  • 举报
回复
给你一个:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace xx
{
/// <summary>
/// Event Handler for SubItem events
/// </summary>
public delegate void SubItemEventHandler(object sender, SubItemEventArgs e);
/// <summary>
/// Event Handler for SubItemEndEditing events
/// </summary>
public delegate void SubItemEndEditingEventHandler(object sender, SubItemEndEditingEventArgs e);

/// <summary>
/// Event Args for SubItemClicked event
/// </summary>
public class SubItemEventArgs : EventArgs
{
public SubItemEventArgs(ListViewItem item, int subItem)
{
_subItemIndex = subItem;
_item = item;
}
private int _subItemIndex = -1;
private ListViewItem _item = null;
public int SubItem
{
get { return _subItemIndex; }
}
public ListViewItem Item
{
get { return _item; }
}
}


/// <summary>
/// Event Args for SubItemEndEditingClicked event
/// </summary>
public class SubItemEndEditingEventArgs : SubItemEventArgs
{
private string _text = string.Empty;
private bool _cancel = true;

public SubItemEndEditingEventArgs(ListViewItem item, int subItem, string display, bool cancel) :
base(item, subItem)
{
_text = display;
_cancel = cancel;
}
public string DisplayText
{
get { return _text; }
set { _text = value; }
}
public bool Cancel
{
get { return _cancel; }
set { _cancel = value; }
}
}


/// <summary>
/// Inherited ListView to allow in-place editing of subitems
/// </summary>
public class ListViewEx : System.Windows.Forms.ListView
{
#region Interop structs, imports and constants
/// <summary>
/// MessageHeader for WM_NOTIFY
/// </summary>
private struct NMHDR
{
public IntPtr hwndFrom;
public Int32 idFrom;
public Int32 code;
}


[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wPar, IntPtr lPar);
[DllImport("user32.dll", CharSet=CharSet.Ansi)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int len, ref int [] order);

// ListView messages
private const int LVM_FIRST = 0x1000;
private const int LVM_GETCOLUMNORDERARRAY = (LVM_FIRST + 59);

// Windows Messages that will abort editing
private const int WM_HSCROLL = 0x114;
private const int WM_VSCROLL = 0x115;
private const int WM_SIZE = 0x05;
private const int WM_NOTIFY = 0x4E;

private const int HDN_FIRST = -300;
private const int HDN_BEGINDRAG = (HDN_FIRST-10);
private const int HDN_ITEMCHANGINGA = (HDN_FIRST-0);
private const int HDN_ITEMCHANGINGW = (HDN_FIRST-20);
#endregion

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public event SubItemEventHandler SubItemClicked;
public event SubItemEventHandler SubItemBeginEditing;
public event SubItemEndEditingEventHandler SubItemEndEditing;

public ListViewEx()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

base.FullRowSelect = true;
base.View = View.Details;
base.AllowColumnReorder = true;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#region Component 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()
{
components = new System.ComponentModel.Container();
}
#endregion

private bool _doubleClickActivation = false;
/// <summary>
/// Is a double click required to start editing a cell?
/// </summary>
public bool DoubleClickActivation
{
get { return _doubleClickActivation; }
set { _doubleClickActivation = value; }
}


/// <summary>
/// Retrieve the order in which columns appear
/// </summary>
/// <returns>Current display order of column indices</returns>
public int[] GetColumnOrder()
{
IntPtr lPar = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * Columns.Count);

IntPtr res = SendMessage(Handle, LVM_GETCOLUMNORDERARRAY, new IntPtr(Columns.Count), lPar);
if (res.ToInt32() == 0) // Something went wrong
{
Marshal.FreeHGlobal(lPar);
return null;
}

int [] order = new int[Columns.Count];
Marshal.Copy(lPar, order, 0, Columns.Count);

Marshal.FreeHGlobal(lPar);

return order;
}


/// <summary>
/// Find ListViewItem and SubItem Index at position (x,y)
/// </summary>
/// <param name="x">relative to ListView</param>
/// <param name="y">relative to ListView</param>
/// <param name="item">Item at position (x,y)</param>
/// <returns>SubItem index</returns>
public int GetSubItemAt(int x, int y, out ListViewItem item)
{
item = this.GetItemAt(x, y);

if (item != null)
{
int[] order = GetColumnOrder();
Rectangle lviBounds;
int subItemX;

lviBounds = item.GetBounds(ItemBoundsPortion.Entire);
subItemX = lviBounds.Left;
for (int i=0; i<order.Length; i++)
{
ColumnHeader h = this.Columns[order[i]];
if (x < subItemX+h.Width)
{
return h.Index;
}
subItemX += h.Width;
}
}

return -1;
}


/// <summary>
/// Get bounds for a SubItem
/// </summary>
/// <param name="Item">Target ListViewItem</param>
/// <param name="SubItem">Target SubItem index</param>
/// <returns>Bounds of SubItem (relative to ListView)</returns>
public Rectangle GetSubItemBounds(ListViewItem Item, int SubItem)
{
int[] order = GetColumnOrder();

Rectangle subItemRect = Rectangle.Empty;
if (SubItem >= order.Length)
throw new IndexOutOfRangeException("SubItem "+SubItem+" out of range");

if (Item == null)
throw new ArgumentNullException("Item");

Rectangle lviBounds = Item.GetBounds(ItemBoundsPortion.Entire);
int subItemX = lviBounds.Left;

ColumnHeader col;
int i;
for (i=0; i<order.Length; i++)
{
col = this.Columns[order[i]];
if (col.Index == SubItem)
break;
subItemX += col.Width;
}
subItemRect = new Rectangle(subItemX, lviBounds.Top, this.Columns[order[i]].Width, lviBounds.Height);
return subItemRect;
}
sunxianyu 2006-06-01
  • 打赏
  • 举报
回复
晕,发那么长代码,险些要了我的小命*_*,
yiming0755 2006-06-01
  • 打赏
  • 举报
回复
很简单,放分吧
TextBox newTb=new TextBox();
this.ListView.cells[列索引值].controls.Add(newTb);
Knight94 2006-06-01
  • 打赏
  • 举报
回复
参看
http://www.codeproject.com/cs/miscctrl/ListViewEmbeddedControls.asp
shiyubeijing 2006-05-31
  • 打赏
  • 举报
回复
对呀,只要能实现功能就行了嘛
Firestone2003 2006-05-31
  • 打赏
  • 举报
回复
你可以使用弹出对话框等其他形式代替,非要在ListView里添加吗?

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧