如何将焦点从数据网格转移到按钮

本文关键字:网格 转移 按钮 数据网 数据 焦点 | 更新日期: 2023-09-27 18:02:49

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
}

我已经包括了一个自定义数据网格和按钮,我无法编写代码将焦点从数据网格转移到按钮。默认情况下,数据网格或按钮将具有焦点。有人能帮我吗?我想在按ENter键时转移焦点。

提前谢谢。

如何将焦点从数据网格转移到按钮

在数据网格上使用KeyPressed事件,检查是否按下Enter键,然后按下焦点按钮。如果你没有什么特别的问题,这应该可以工作:

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