DataGridView.ProcessDataGridViewKey(KeyEventArgs) 方法

定义

处理用于在 . 中导航的 DataGridView键。

protected:
 virtual bool ProcessDataGridViewKey(System::Windows::Forms::KeyEventArgs ^ e);
protected virtual bool ProcessDataGridViewKey(System.Windows.Forms.KeyEventArgs e);
abstract member ProcessDataGridViewKey : System.Windows.Forms.KeyEventArgs -> bool
override this.ProcessDataGridViewKey : System.Windows.Forms.KeyEventArgs -> bool
Protected Overridable Function ProcessDataGridViewKey (e As KeyEventArgs) As Boolean

参数

e
KeyEventArgs

包含有关按下的键的信息。

返回

true 如果已处理密钥,则为否则,为 false.

例外

按下键会导致控件进入编辑模式,但 EditType 当前单元格的属性并不指示派生 Control 自和实现 IDataGridViewEditingControl的类。

此操作将提交单元格值或进入编辑模式,但数据源中的错误会阻止操作,并且没有事件的处理程序 DataError 或处理程序已将 ThrowException 属性 true设置为 。

-或-

DELETE 键将删除一行或多行,但数据源中的错误会阻止删除,并且没有事件的处理程序 DataError 或处理程序将 ThrowException 属性 true设置为 。

示例

下面的代码示例演示如何通过重写DataGridViewProcessDataGridViewKey方法更改子类中 ProcessDialogKey ENTER 键的行为。 在此示例中,ENTER 键的行为与向右键相同,使用户更容易编辑单个数据行中的多个单元格。

public class CustomDataGridView : DataGridView
{
    protected override bool ProcessDialogKey(Keys keyData)
    {
        // Extract the key code from the key value. 
        Keys key = (keyData & Keys.KeyCode);

        // Handle the ENTER key as if it were a RIGHT ARROW key. 
        if (key == Keys.Enter)
        {
            return this.ProcessRightKey(keyData);
        }
        return base.ProcessDialogKey(keyData);
    }

    protected override bool ProcessDataGridViewKey(KeyEventArgs e)
    {
        // Handle the ENTER key as if it were a RIGHT ARROW key. 
        if (e.KeyCode == Keys.Enter)
        {
            return this.ProcessRightKey(e.KeyData);
        }
        return base.ProcessDataGridViewKey(e);
    }
}
Public Class CustomDataGridView
    Inherits DataGridView

    <System.Security.Permissions.UIPermission( _
        System.Security.Permissions.SecurityAction.LinkDemand, _
        Window:=System.Security.Permissions.UIPermissionWindow.AllWindows)> _
    Protected Overrides Function ProcessDialogKey( _
        ByVal keyData As Keys) As Boolean

        ' Extract the key code from the key value. 
        Dim key As Keys = keyData And Keys.KeyCode

        ' Handle the ENTER key as if it were a RIGHT ARROW key. 
        If key = Keys.Enter Then
            Return Me.ProcessRightKey(keyData)
        End If

        Return MyBase.ProcessDialogKey(keyData)

    End Function

    <System.Security.Permissions.SecurityPermission( _
        System.Security.Permissions.SecurityAction.LinkDemand, Flags:= _
        System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _
    Protected Overrides Function ProcessDataGridViewKey( _
        ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean

        ' Handle the ENTER key as if it were a RIGHT ARROW key. 
        If e.KeyCode = Keys.Enter Then
            Return Me.ProcessRightKey(e.KeyData)
        End If

        Return MyBase.ProcessDataGridViewKey(e)

    End Function

End Class

注解

此方法调用适用于按下键的键处理方法(例如 ProcessF2Key ,按下 F2 时的方法),并返回该方法的返回值。

继承者说明

重写此方法时,控件应返回 true 以指示它已处理密钥。 对于控件未处理的键,返回此方法的基本版本的结果。

适用于

另请参阅