DataGridView.ProcessDialogKey(Keys) 方法

定義

處理用來控制對話方塊的按鍵,例如 TAB、ESCAPE、ENTER 和方向鍵。

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 值的位元 (Bitwise) 組合,表示要處理的按鍵。

傳回

如果已處理按鍵,則為 true;否則為 false

例外狀況

按下按鍵會讓控制項進入編輯模式,但目前儲存格的 EditType 屬性不會指出衍生自 Control 的類別並會實作 IDataGridViewEditingControl

這個動作會認可儲存格值或進入編輯模式,但是資料來源中的錯誤會阻止這個動作,而且 DataError 事件沒有處理常式或處理常式已將 ThrowException 屬性設定為 true

範例

下列程式碼範例示範如何覆 ProcessDataGridViewKey 寫 和 ProcessDialogKey 方法,以變更子類別中 DataGridView 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

備註

這個方法會在編輯模式中呼叫,以處理主控編輯控制項未處理的鍵盤輸入。

如果按鍵按下決定不是輸入鍵,則會傳送至基類進行處理。

給繼承者的注意事項

覆寫此方法時,控制項應該會傳回 true ,以指出它已處理索引鍵。 對於控制項未處理的索引鍵,傳回這個方法基底版本的結果。

適用於

另請參閱