Back to Devexpress

BaseView.KeyDown Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-baseview-1a858366.md

latest8.5 KB
Original Source

BaseView.KeyDown Event

Fires when a user presses a key while the View has focus.

Namespace : DevExpress.XtraGrid.Views.Base

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Key")]
public event KeyEventHandler KeyDown
vb
<DXCategory("Key")>
Public Event KeyDown As KeyEventHandler

Event Data

The KeyDown event's data class is KeyEventArgs. The following properties provide information specific to this event:

PropertyDescription
AltGets a value indicating whether the ALT key was pressed.
ControlGets a value indicating whether the CTRL key was pressed.
HandledGets or sets a value indicating whether the event was handled.
KeyCodeGets the keyboard code for a KeyDown or KeyUp event.
KeyDataGets the key data for a KeyDown or KeyUp event.
KeyValueGets the keyboard value for a KeyDown or KeyUp event.
ModifiersGets the modifier flags for a KeyDown or KeyUp event. The flags indicate which combination of CTRL, SHIFT, and ALT keys was pressed.
ShiftGets a value indicating whether the SHIFT key was pressed.
SuppressKeyPressGets or sets a value indicating whether the key event should be passed on to the underlying control.

Remarks

If a user holds a key for a while, the event occurs repeatedly with a short interval. Thus you can handle the event to implement recurrent actions. If you don’t want to process recurrent KeyDown events, set a flag once you’ve performed the desired actions. You will reset this flag in the BaseView.KeyUp event handler when a user releases the key:

csharp
bool keyDownReleased = true;
gridView1.KeyDown += (sender, e) =>
{
    if (keyDownReleased)
    {
        //Perform the necessary actions
        keyDownReleased = false;
    }
};
gridView1.KeyUp += (sender, e) =>
{
    keyDownReleased = true;
};
vb
Dim keyDownReleased As Boolean = True
AddHandler gridView1.KeyDown, Sub(sender, e)
    If keyDownReleased Then
        'Perform the necessary actions
        keyDownReleased = False
    End If
End Sub
AddHandler gridView1.KeyUp, Sub(sender, e)
    keyDownReleased = True
End Sub

As soon as the KeyDown event has occurred, the View generates a BaseView.KeyPress event if a character key is pressed. The BaseView.KeyPress event does not occur for non-character keys.

If an in-place editor is active, it handles key presses. In this case, the View’s key events do not occur.

The following code deletes the focused row when the end-user presses the Ctrl+Del shortcut.

To process key press events, we handle the BaseView.KeyDown event. The row is deleted by calling the ColumnView.DeleteRow method.

csharp
private void gridView1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
    if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.Control) {
        if (MessageBox.Show("Delete row?", "Confirmation", MessageBoxButtons.YesNo) != 
          DialogResult.Yes)
            return;
        GridView view = sender as GridView;                
        view.DeleteRow(view.FocusedRowHandle);
    }
}
vb
Private Sub GridView1_KeyDown(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridView1.KeyDown
    If (e.KeyCode = Keys.Delete And e.Modifiers = Keys.Control) Then
        If (MessageBox.Show("Delete row?", "Confirmation", _
          MessageBoxButtons.YesNo) <> DialogResult.Yes) Then Return
        Dim view As GridView = CType(sender, GridView)
        view.DeleteRow(view.FocusedRowHandle)
    End If
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the KeyDown event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-grid-multiple-row-selection-web-style-checkboxes/CS/E1271/CheckMarkSelection.cs#L133

csharp
view.CustomUnboundColumnData += new CustomColumnDataEventHandler(view_CustomUnboundColumnData);
view.KeyDown += new KeyEventHandler(view_KeyDown);
view.RowStyle += new RowStyleEventHandler(view_RowStyle);

winforms-grid-multiple-row-selection-web-style-checkboxes/VB/E1271/CheckMarkSelection.vb#L161

vb
AddHandler view.CustomUnboundColumnData, New CustomColumnDataEventHandler(AddressOf view_CustomUnboundColumnData)
AddHandler view.KeyDown, New KeyEventHandler(AddressOf view_KeyDown)
AddHandler view.RowStyle, New RowStyleEventHandler(AddressOf view_RowStyle)

See Also

ProcessGridKey

KeyPress

KeyUp

BaseView Class

BaseView Members

DevExpress.XtraGrid.Views.Base Namespace