共用方式為


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

範例

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

備註

這個方法會呼叫適合按下 (按鍵的按鍵處理方法,例如, ProcessF2Key 按下 F2 時的方法會) ,並傳回該方法的傳回值。

給繼承者的注意事項

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

適用於

另請參閱