您的位置:首页 > 移动开发 > Objective-C

为 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列进行了特殊对待。

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  listview object c#