如何将焦点从datagrid转移到按钮?

时间:2011-07-19 09:48:47

标签: c# button datagrid user-controls

protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
    RectangleF textBounds;// Bounds of text 
    Object cellData;// Object to show in the cell 

    DrawBackground(g, bounds, rowNum, backBrush);// Draw cell background

    bounds.Inflate(-2, -2);// Shrink cell by couple pixels for text.

    textBounds = new RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height);
    // Set text bounds.
    cellData = this.PropertyDescriptor.GetValue(source.List[rowNum]);   // Get data for this cell from data source.

    g.DrawString(FormatText(cellData), this.Owner.Font, foreBrush, textBounds, this.StringFormat);
    // Render contents 
    this.updateHostedControl();// Update floating hosted control.
}

// Would reposition, hide and show hosted control as needed.
protected void updateHostedControl()
{
    Rectangle selectedBounds = this.Owner.GetCellBounds(this.Owner.CurrentCell.RowNumber, this.Owner.CurrentCell.ColumnNumber);
    // Get selected cell bounds.
    // We only need to show hosted control if column is not read only, 
    // selected cell is in our column and not occluded by anything.

    if (!this.ReadOnly && (this.ColumnOrdinal == this.Owner.CurrentCell.ColumnNumber) &&
         this.Owner.HitTest(selectedBounds.Left, selectedBounds.Top).Type == DataGrid.HitTestType.Cell &&
         this.Owner.HitTest(selectedBounds.Right, selectedBounds.Bottom).Type == DataGrid.HitTestType.Cell)
    {
        if (selectedBounds != this._bounds)// See if control bounds are already set.
        {
            this._bounds = selectedBounds; // Store last bounds. Note we can't use control's bounds 
            // as some controls are not resizable and would change bounds as they pleased.
            this.HostedControl.Bounds = selectedBounds;// Update control bounds.

            this.HostedControl.Focus();
            this.HostedControl.Update();// And update control now so it looks better visually.
        }

        if (!this.HostedControl.Visible)// If control is not yet visible...
        {
            this.HostedControl.Show();// Show it
            this.HostedControl.Focus();
        }
    }
    else if (this.HostedControl.Visible)// Hosted control should not be visible. Check if it is.
    {
        this.HostedControl.Hide();// Hide it.
    }

}

protected virtual void DrawBackground(Graphics g, Rectangle bounds, int rowNum, Brush backBrush)
{
    Brush background = backBrush;// Use default brush by... hmm... default.

    if ((null != background) && ((rowNum & 1) != 0) && !Owner.IsSelected(rowNum))
    {                                                                   
        // If have alternating brush, row is odd and not selected...
        background = _alternatingBrush;// Then use alternating brush.
    }
    else
        background = new SolidBrush(Color.White);

    g.FillRectangle(background, bounds);// Draw cell background
}

我已经包含了一个自定义数据网格和按钮,我无法编写代码将焦点从数据网格传输到按钮。默认数据网格或按钮将具有焦点。请任何人帮我这个。我想在按下EN键的时候转移焦点。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

在datagrid上使用KeyPressed事件,选中按Enter键然后按下焦点按钮。如果您没有任何具体问题,这应该有用:

private void datagrid_KeyPress(object sender, KeyPressEventArgs e)
{
   if (e.KeyChar == (char)Keys.Enter)
      button1.Focus();
}