Back to Devexpress

BaseView.KeyPress Event

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

latest5.2 KB
Original Source

BaseView.KeyPress Event

Fires when a character key is pressed 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 KeyPressEventHandler KeyPress
vb
<DXCategory("Key")>
Public Event KeyPress As KeyPressEventHandler

Event Data

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

PropertyDescription
HandledGets or sets a value indicating whether the KeyPress event was handled.
KeyCharGets or sets the character corresponding to the key pressed.

Remarks

When pressing character a key, the KeyPress event is raised. If holding the key pressed for a while, the event is raised repeatedly with a short interval. Thus, you can handle the event to implement repeated actions. However, if you do not want to perform repeated actions, you need to have a flag indicating whether the desired actions have already been performed. This flag must be reset in the BaseView.KeyUp event handler. The BaseView.KeyUp event is raised when the pressed key is released.

The KeyPress event is raised immediately after the BaseView.KeyDown event if a character key is pressed. If pressing a non-character key, only the BaseView.KeyDown event is rased.

If an inplace editor is active, it processes key presses. In this case, the View’s key events are not raised.

Example

The following example shows how to prevent numeric characters (‘0’-‘9’) from being entered within a grid’s CustomerID column. Two events are handled to prevent entering these characters:

  • the BaseView.KeyPress event - fires when an alpha numeric character is pressed while an in-place editor is not active;
  • the grid control’s EditorContainer.EditorKeyPress event - fires when an alpha numeric character is pressed while an in-place editor is active.

If a specific character needs to be discarded in these event handlers, the Handled parameter must be set to true.

csharp
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

// Fires when no in-place editor is active
private void gridView1_KeyPress(object sender, KeyPressEventArgs e) {
    GridView view = sender as GridView;
    string s = "0123456789";
    if (view.FocusedColumn.FieldName == "CustomerID" && s.IndexOf(e.KeyChar) >= 0)
        e.Handled = true;
}
// Fires when an in-place editor is active
private void gridControl1_EditorKeyPress(object sender, KeyPressEventArgs e) {
    GridControl grid = sender as GridControl;
    gridView1_KeyPress(grid.FocusedView, e);
}
vb
Imports System.Windows.Forms
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid

' Fires when no in-place editor is active.
Private Sub GridView1_KeyPress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) _
  Handles GridView1.KeyPress
    Dim view As GridView = CType(sender, GridView)
    Dim s As String = "0123456789"
    If view.FocusedColumn.FieldName = "CustomerID" And s.IndexOf(e.KeyChar) >= 0 Then
        e.Handled = True
    End If
End Sub

' Fires when an in-place editor is active.
Private Sub GridControl1_EditorKeyPress(ByVal sender As System.Object, _
  ByVal e As KeyPressEventArgs) _
  Handles GridControl1.EditorKeyPress
    Dim grid As GridControl = CType(sender, GridControl)
    GridView1_KeyPress(grid.FocusedView, e)
End Sub

See Also

ProcessGridKey

KeyDown

KeyUp

BaseView Class

BaseView Members

DevExpress.XtraGrid.Views.Base Namespace