类的代码在这里大家来看看吧 using System; using System.Windows.Forms; using System.Data; using System.Drawing; using UtilityLibrary.Win32;namespace upload { /// <summary> /// ClassExtendLv 的摘要说明。 /// </summary> /// <summary> /// 列风格枚举 /// </summary> public enum agu_ListViewColumnStyle { ReadOnly, //只读 EditBox, //编辑状态下显示为文本框 ComboBox //编辑状态下显示为组合框 }
public class agu_ColumnHeader: ColumnHeader { private agu_ListViewColumnStyle cs;//本列的风格
public agu_ColumnHeader():base() { cs = agu_ListViewColumnStyle.ReadOnly; } public agu_ColumnHeader(agu_ListViewColumnStyle _cs) { cs=_cs; } public agu_ListViewColumnStyle ColumnStyle { get {return cs;} set {cs=value;} }
} public class ClassExtendLv: 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 TextBox editBox; private Font m_fontComboBox; private Font m_fontEdit; private Color m_bgcolorComboBox; private Color m_bgcolorEdit; public ClassExtendLv() { editBox =new TextBox(); this.m_fontComboBox=this.Font; this.EditFont=this.Font;
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, agu_ListViewColumnStyle cs) { if (columnIndex<0 || columnIndex>this.Columns.Count) throw new Exception("Column index is out of range"); ((agu_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 ( ((agu_ColumnHeader)Columns[columnIndex]).ColumnStyle != agu_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 Size(0,0); newbox.Location = new Point(0,0); this.Controls.AddRange(new Control[] {newbox}); newbox.SelectedIndexChanged += new EventHandler(this.CmbSelected); newbox.LostFocus += new EventHandler(this.CmbFocusOver); newbox.KeyPress += new KeyPressEventHandler(this.CmbKeyPress); newbox.Font = this.ComboBoxFont; newbox.BackColor = this.ComboBoxBgColor; newbox.DropDownStyle = ComboBoxStyle.DropDownList; newbox.Hide(); this.m_arrComboBoxes[columnIndex] = newbox; }
editBox.Hide(); } Rectangle GetSubItemRect(int row, int col) { //top The one-based index of the subitem //left Flag value (see following). Indicates the portion of the list view subitem for which to retrieve the bounding rectangle. RECT rc = new RECT(); rc.top = col;//rc.top为 rc.left = (int)SubItemPortion.LVIR_BOUNDS; WindowsAPI.SendMessage(Handle, (int)ListViewMessages.LVM_GETSUBITEMRECT, row, ref rc);
if ( col == 0 ) { return new Rectangle(0,0,0,0); // The LVM_GETSUBITEMRECT message does not give us the rectangle for the first subitem // since it is not considered a subitem // obtain the rectangle for the header control and calculate from there // Rectangle headerRect = GetHeaderItemRect(col); // return new Rectangle(rc.left, rc.top, headerRect.Width, rc.bottom-rc.top); }
return new Rectangle(rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top); } public void SMKDoubleClick(object sender,System.EventArgs e) { int nStart=m_nX; int spos=0; int epos=this.Columns[0].Width; int width=0; ListViewItem listItem=FocusedItem; int row=listItem.Index; int i=0; for (;i<this.Columns.Count;i++) { width=this.Columns[i].Width; if (nStart > spos && nStart < epos) { m_nSubItemSelected = i; break; } spos = epos; epos += this.Columns[i].Width; } m_strSubItemText = m_currentLVItem.SubItems[m_nSubItemSelected].Text; agu_ColumnHeader column = (agu_ColumnHeader)Columns[m_nSubItemSelected]; if (column.ColumnStyle == agu_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 = GetSubItemRect(row,i); //※※※※※※※以前代码※※※※※※※※※ //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.Size = new System.Drawing.Size(width, m_currentLVItem.Bounds.Bottom-m_currentLVItem.Bounds.Top); cmbBox.Location = new Point(spos , m_currentLVItem.Bounds.Y); cmbBox.Show(); cmbBox.Text = m_strSubItemText; cmbBox.SelectAll(); cmbBox.Focus(); } if (column.ColumnStyle == agu_ListViewColumnStyle.EditBox) { Rectangle r = GetSubItemRect(row,i); //※※※※※※※以前代码※※※※※※※※※ //Rectangle r = new Rectangle(spos , m_currentLVItem.Bounds.Y , epos , m_currentLVItem.Bounds.Bottom); //editBox.Size = new Size(epos - spos , m_currentLVItem.Bounds.Height); //※※※※※※※以前代码※※※※※※※※※ editBox.Size = new Size(width , m_currentLVItem.Bounds.Height); editBox.Location = new 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; }
private void FocusOver (object sender,EventArgs e)
{
m_currentLVItem.SubItems[m_nSubItemSelected].Text=editBox.Text;
editBox.Hide(); }
就这个地方有问题!
1。LISTVIEW里嵌套的TEXTBOX和COMBOBOX的位置无法定位好,当然这我已经修改好了
2。如果你在嵌套有TEXTBOX的LISTVIEW列中双击一个单元格,(其实就是一个TEXTBOX)你在这个TEXTBOX里填写好TEXT后,在单击同列的其他行时,就会发现刚才的TEXT填写到另外的TEXTBOX里了。
using System;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using UtilityLibrary.Win32;namespace upload
{
/// <summary>
/// ClassExtendLv 的摘要说明。
/// </summary>
/// <summary>
/// 列风格枚举
/// </summary>
public enum agu_ListViewColumnStyle
{
ReadOnly, //只读
EditBox, //编辑状态下显示为文本框
ComboBox //编辑状态下显示为组合框
}
public class agu_ColumnHeader: ColumnHeader
{
private agu_ListViewColumnStyle cs;//本列的风格
public agu_ColumnHeader():base()
{
cs = agu_ListViewColumnStyle.ReadOnly;
} public agu_ColumnHeader(agu_ListViewColumnStyle _cs)
{
cs=_cs;
} public agu_ListViewColumnStyle ColumnStyle
{
get
{return cs;}
set
{cs=value;}
}
}
public class ClassExtendLv: 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 TextBox editBox;
private Font m_fontComboBox;
private Font m_fontEdit;
private Color m_bgcolorComboBox;
private Color m_bgcolorEdit; public ClassExtendLv()
{
editBox =new TextBox();
this.m_fontComboBox=this.Font;
this.EditFont=this.Font;
this.EditBgColor=Color.LightBlue;
this.m_bgcolorComboBox=Color.LightBlue;
this.MouseDown +=new MouseEventHandler(this.SMKMouseDown);
this.DoubleClick +=new EventHandler(this.SMKDoubleClick);
this.GridLines=true; editBox.Size=new Size(0,0);
editBox.Location=new Point(0,0);
this.Controls.AddRange(new Control[] {this.editBox});
editBox.KeyPress+=new KeyPressEventHandler(this.EditOver);
editBox.LostFocus+=new 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, agu_ListViewColumnStyle cs)
{
if (columnIndex<0 || columnIndex>this.Columns.Count)
throw new Exception("Column index is out of range");
((agu_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 ( ((agu_ColumnHeader)Columns[columnIndex]).ColumnStyle != agu_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 Size(0,0);
newbox.Location = new Point(0,0);
this.Controls.AddRange(new Control[] {newbox});
newbox.SelectedIndexChanged += new EventHandler(this.CmbSelected);
newbox.LostFocus += new EventHandler(this.CmbFocusOver);
newbox.KeyPress += new 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 , KeyPressEventArgs e)
{
ComboBox cmbBox = (ComboBox)sender;
if ( e.KeyChar == 13 || e.KeyChar == 27 ) //CR or ESC press
{
cmbBox.Hide();
}
}
private void CmbSelected(object sender , 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,KeyPressEventArgs e)
{
if (e.KeyChar==13)
{
m_currentLVItem.SubItems[m_nSubItemSelected].Text=editBox.Text;
editBox.Hide();
}
if (e.KeyChar==27)
editBox.Hide();
}
{
System.Diagnostics.Debug.WriteLine("FocusOver");
m_currentLVItem.SubItems[m_nSubItemSelected].Text=editBox.Text;
editBox.Hide(); } Rectangle GetSubItemRect(int row, int col)
{
//top The one-based index of the subitem
//left Flag value (see following). Indicates the portion of the list view subitem for which to retrieve the bounding rectangle. RECT rc = new RECT();
rc.top = col;//rc.top为
rc.left = (int)SubItemPortion.LVIR_BOUNDS;
WindowsAPI.SendMessage(Handle, (int)ListViewMessages.LVM_GETSUBITEMRECT, row, ref rc);
if ( col == 0 )
{
return new Rectangle(0,0,0,0);
// The LVM_GETSUBITEMRECT message does not give us the rectangle for the first subitem
// since it is not considered a subitem
// obtain the rectangle for the header control and calculate from there
// Rectangle headerRect = GetHeaderItemRect(col);
// return new Rectangle(rc.left, rc.top, headerRect.Width, rc.bottom-rc.top);
}
return new Rectangle(rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
}
public void SMKDoubleClick(object sender,System.EventArgs e)
{
int nStart=m_nX;
int spos=0;
int epos=this.Columns[0].Width;
int width=0;
ListViewItem listItem=FocusedItem;
int row=listItem.Index;
int i=0;
for (;i<this.Columns.Count;i++)
{
width=this.Columns[i].Width;
if (nStart > spos && nStart < epos)
{
m_nSubItemSelected = i;
break;
}
spos = epos;
epos += this.Columns[i].Width;
}
m_strSubItemText = m_currentLVItem.SubItems[m_nSubItemSelected].Text;
agu_ColumnHeader column = (agu_ColumnHeader)Columns[m_nSubItemSelected]; if (column.ColumnStyle == agu_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 = GetSubItemRect(row,i);
//※※※※※※※以前代码※※※※※※※※※
//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.Size = new System.Drawing.Size(width, m_currentLVItem.Bounds.Bottom-m_currentLVItem.Bounds.Top);
cmbBox.Location = new Point(spos , m_currentLVItem.Bounds.Y);
cmbBox.Show();
cmbBox.Text = m_strSubItemText;
cmbBox.SelectAll();
cmbBox.Focus();
} if (column.ColumnStyle == agu_ListViewColumnStyle.EditBox)
{
Rectangle r = GetSubItemRect(row,i);
//※※※※※※※以前代码※※※※※※※※※
//Rectangle r = new Rectangle(spos , m_currentLVItem.Bounds.Y , epos , m_currentLVItem.Bounds.Bottom);
//editBox.Size = new Size(epos - spos , m_currentLVItem.Bounds.Height);
//※※※※※※※以前代码※※※※※※※※※
editBox.Size = new Size(width , m_currentLVItem.Bounds.Height);
editBox.Location = new 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;
}
}
}