为 ListView 控件增加动态编辑功能
2011-01-01 22:49
274 查看
默认的 ListView 控件没有动态编辑功能,通常的做法是生成一个 Edit 或 ComboBox 控件覆盖在指定的位置。
C# 封装的控件在实现这一功能却不那么直接了。
参考了 codeproject 和其他网页的代码,勉强写了一个,但始终有些问题。
判断覆盖的区域问题较多,实现的主要代码为
private void CLVMouseDown(object sender, MouseEventArgs e)
{
this.lvi = this.GetItemAt(e.X , e.Y);
this.pt = new Point(e.X, e.Y);
}
private void CLVDoubleClick(object sender, System.EventArgs e)
{
// Check the subitem clicked .
int nStart = this.pt.X ;
int spos = 0 ;
int epos = this.Columns[0].Width ;
for ( int i=0; i < this.Columns.Count ; i++)
{
if ( nStart > spos && nStart < epos )
{
nSubItemSelected = i ;
break;
}
spos = epos ;
epos += this.Columns[i].Width;
}
this.SubItemText = this.lvi.SubItems[nSubItemSelected].Text ;
Rectangle r = new Rectangle(spos, this.lvi.Bounds.Y, epos, this.lvi.Bounds.Bottom);
this.EmEdit.Size = new Size(epos - spos, this.lvi.Bounds.Bottom - this.lvi.Bounds.Top);
this.EmEdit.Location = new Point(spos, this.lvi.Bounds.Y);
//...
}
主要问题是点击后,获取的 subitem 或者其显示的区域不准确,经常出问题。
后来看到了利用 LVM_GETSUBITEMRECT 的方法,但编辑第一列会导致覆盖整行的区域
再仔细瞅瞅显示的代码,原来是这么处理的
// Set the size of the control(combobox/editbox)
// This should be composed of the height of the current items and
// width of the current column
Size sz = new Size(this.Columns[col].Width, Items[row].Bounds.Height );
// Determine the location where the control(combobox/editbox) to be placed
Point location = col == 0 ? new Point(0, rect.top) : new Point( rect.left, rect.top);
看到了吧,宽度用的是列宽,并且显示位置对第0列进行了特殊对待。
C# 封装的控件在实现这一功能却不那么直接了。
参考了 codeproject 和其他网页的代码,勉强写了一个,但始终有些问题。
判断覆盖的区域问题较多,实现的主要代码为
private void CLVMouseDown(object sender, MouseEventArgs e)
{
this.lvi = this.GetItemAt(e.X , e.Y);
this.pt = new Point(e.X, e.Y);
}
private void CLVDoubleClick(object sender, System.EventArgs e)
{
// Check the subitem clicked .
int nStart = this.pt.X ;
int spos = 0 ;
int epos = this.Columns[0].Width ;
for ( int i=0; i < this.Columns.Count ; i++)
{
if ( nStart > spos && nStart < epos )
{
nSubItemSelected = i ;
break;
}
spos = epos ;
epos += this.Columns[i].Width;
}
this.SubItemText = this.lvi.SubItems[nSubItemSelected].Text ;
Rectangle r = new Rectangle(spos, this.lvi.Bounds.Y, epos, this.lvi.Bounds.Bottom);
this.EmEdit.Size = new Size(epos - spos, this.lvi.Bounds.Bottom - this.lvi.Bounds.Top);
this.EmEdit.Location = new Point(spos, this.lvi.Bounds.Y);
//...
}
主要问题是点击后,获取的 subitem 或者其显示的区域不准确,经常出问题。
后来看到了利用 LVM_GETSUBITEMRECT 的方法,但编辑第一列会导致覆盖整行的区域
再仔细瞅瞅显示的代码,原来是这么处理的
// Set the size of the control(combobox/editbox)
// This should be composed of the height of the current items and
// width of the current column
Size sz = new Size(this.Columns[col].Width, Items[row].Bounds.Height );
// Determine the location where the control(combobox/editbox) to be placed
Point location = col == 0 ? new Point(0, rect.top) : new Point( rect.left, rect.top);
看到了吧,宽度用的是列宽,并且显示位置对第0列进行了特殊对待。
相关文章推荐
- Redrain 通用菜单控件使用方法和说明(增加动态添加功能、附源码和demo)
- Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能
- ListView视图动态增加与删除控件条目,内容
- 让listview增加编辑功能
- Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能
- Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能
- Redrain 通用菜单控件使用方法和说明(增加动态添加功能、附源码和demo)
- Android仿微信滑动弹出编辑、删除菜单效果、增加下拉刷新功能
- AngularJS:实现动态添加输入控件功能
- 动态增加和删除多个web用户控件
- Android实现频道列表ListView的编辑/删除/移动等功能
- android 动态显示隐藏listview的headerview和headerview里的控件
- 增加 MsFlexGrid 的编辑功能
- 用C++Builder开发具有历史记录功能的编辑控件
- ListView 动态增加列标题
- android ListView item里动态增加多个LinearLayout的问题
- DeltaOffice word在线编辑控件增加OnlyReading只读接口
- 扩展GridView控件(索引) - 增加多个常用功能
- 扩展jQuery easyui datagrid增加动态改变列编辑的类型
- Listview控件使用SimpleAdapter适配器实现屏幕下滑增加一个item选项以及用到的方法总结