DataGridView.ProcessDialogKey(Keys) 方法

定义

处理用来控制对话框的键,例如 Tab、Esc、Enter 和箭头键。Processes keys, such as the TAB, ESCAPE, ENTER, and ARROW keys, used to control dialog boxes.

protected:
 override bool ProcessDialogKey(System::Windows::Forms::Keys keyData);
protected override bool ProcessDialogKey (System.Windows.Forms.Keys keyData);
override this.ProcessDialogKey : System.Windows.Forms.Keys -> bool
Protected Overrides Function ProcessDialogKey (keyData As Keys) As Boolean

参数

keyData
Keys

Keys 值的按位组合,这些值表示要处理的一个或多个键。A bitwise combination of Keys values that represents the key or keys to process.

返回

如果已处理该键,则为 true;否则为 falsetrue if the key was processed; otherwise, false.

异常

按下的键将导致该控件进入编辑模式,但当前单元格的 EditType 属性不指示派生自 Control 并实现 IDataGridViewEditingControl 的类。The key pressed would cause the control to enter edit mode, but the EditType property of the current cell does not indicate a class that derives from Control and implements IDataGridViewEditingControl.

此操作将提交一个单元格值或者进入编辑模式,但是数据源中的错误禁止执行此操作,DataError 事件没有处理程序,或者处理程序已将 ThrowException 属性设置为 trueThis action would commit a cell value or enter edit mode, but an error in the data source prevents the action and either there is no handler for the DataError event or the handler has set the ThrowException property to true.

示例

下面的代码示例演示如何通过重写 ProcessDataGridViewKeyProcessDialogKey 方法,在 @no__t 的子类中更改 ENTER 键的行为。The following code example demonstrates how to change the behavior of the ENTER key in a DataGridView subclass by overriding the ProcessDataGridViewKey and ProcessDialogKey methods. 在此示例中,ENTER 键的行为与向右箭头键相同,使用户可以更轻松地编辑单个数据行中的多个单元格。In the example, the ENTER key has the same behavior as the RIGHT ARROW key, making it easier for a user to edit multiple cells in a single row of data.

public class CustomDataGridView : DataGridView
{
    [System.Security.Permissions.UIPermission(
        System.Security.Permissions.SecurityAction.LinkDemand,
        Window = System.Security.Permissions.UIPermissionWindow.AllWindows)]
    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);
    }

    [System.Security.Permissions.SecurityPermission(
        System.Security.Permissions.SecurityAction.LinkDemand, Flags = 
        System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
    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

注解

此方法在编辑模式下调用,以处理不是由寄宿编辑控件处理的键盘输入。This method is called in edit mode to handle keyboard input that is not handled by the hosted editing control.

如果将按键确定为不是输入键,则会将其发送到基类进行处理。If a key press is determined to not be an input key, it will be sent to the base class for processing.

继承者说明

重写此方法时,控件应返回 true 来指示它已处理该键。When overriding this method, a control should return true to indicate that it has processed the key. 对于不是由控件处理的键,返回此方法的基版本的结果。For keys that are not processed by the control, return the result of the base version of this method.

安全性

UIPermission
用于在按 TAB 键时使用所有窗口和用户事件。for using all windows and user events when the TAB key is pressed. 需求值: DemandDemand value: Demand. 权限值: AllWindowsPermission value:AllWindows

适用于

另请参阅