Category / Section
How to capture function keys when the current cell is in active edit mode in WinForms GridControl?
1 min read
Handle the control key
When the grid cell is not actively edited, the grid itself gets these keystrokes. In this case, you can use event handlers like grid.CurrentCellKeyDown and grid.KeyDown to catch the keys. When the current cell is actively being edited, the grid does not automatically catch these keys.
Solution
When the current cell is actively being edited, you can use the grid.CurrentCellControlKeyMessage to catch the function keys.
C#
//subscribe to the event this.gridDataBoundGrid1.CurrentCellControlKeyMessage += new GridCurrentCellControlKeyMessageEventHandler(grid_CurrentCellControlKeyMessage); private void grid_CurrentCellControlKeyMessage(object sender, GridCurrentCellControlKeyMessageEventArgs e) { Keys keyCode = (Keys)((int)e.Msg.WParam) & Keys.KeyCode; //To view the result in output screen. Console.WriteLine(keyCode); Console.WriteLine(e.Msg); //To display the key pressed. MessageBox.Show(keyCode.ToString() + " is pressed"); }
VB
'subscribe to the event Private Me.gridDataBoundGrid1.CurrentCellControlKeyMessage += New GridCurrentCellControlKeyMessageEventHandler(AddressOf grid_CurrentCellControlKeyMessage) Private Sub grid_CurrentCellControlKeyMessage(ByVal sender As Object, ByVal e As GridCurrentCellControlKeyMessageEventArgs) Dim keyCode As Keys = CType(CInt(Fix(e.Msg.WParam)), Keys) And Keys.KeyCode 'To view the result in output screen. Console.WriteLine(keyCode) Console.WriteLine(e.Msg) 'To display the key pressed. MessageBox.Show(keyCode.ToString() & " is pressed") End Sub
Figure 1: control key handled in cell edit mode.
Samples: