如题:控件添加上去了,但是滑动滚动条的时候位置会随着改变。关键代码如下:
#region 向指定列添加自定义控件 + public static void AddCustControl(this C1FlexGrid flex, int rowIndex, int colIndex, Control control)
/// <summary>
/// 向指定列添加自定义控件
/// </summary>
/// <param name="flex"></param>
/// <param name="rowIndex"></param>
/// <param name="colIndex"></param>
/// <param name="control"></param>
public static void AddCustControl(this C1FlexGrid flex, int rowIndex, int colIndex, Control control, int rowHeight)
{
HostedControl host = new HostedControl(flex, control, rowIndex, colIndex, rowHeight);
host.UpdatePosition();
}
#endregion
/// <summary>
/// HostedControl 添加自定义控件
/// helper class that contains a control hosted within a C1FlexGrid
/// </summary>
internal class HostedControl
{
internal C1FlexGrid _flex;
internal Control _ctl;
internal Row _row;
internal Column _col;
int rowHeight;
internal HostedControl(C1FlexGrid flex, Control hosted, int row, int col, int rowHeight)
{
this.rowHeight = rowHeight;
// save info
_flex = flex;
_ctl = hosted;
_row = flex.Rows[row];
_col = flex.Cols[col];
// insert hosted control into grid
_flex.Controls.Add(_ctl);
}
internal bool UpdatePosition()
{
// get row/col indices
int r = _row.Index;
int c = _col.Index;
if (r < 0 || c < 0) return false;
// get cell rect
Rectangle rc = _flex.GetCellRect(r, c, false);
rc.Y = rowHeight * r + 2;
// hide control if out of range
if (rc.Width <= 0 || rc.Height <= 0 || !rc.IntersectsWith(_flex.ClientRectangle))
{
_ctl.Visible = false;
return true;
}
// move the control and show it
_ctl.Bounds = rc;
_ctl.Visible = true;
// done
return true;
}
}
附图:
|