PreviewKeyDown
This fixes a problem with keyboard input on DataGridView
. If the user focuses a cell and presses enter, the selection might not work properly.
By using PreviewKeyDown
, we can correct an issue with keyboard navigation. If you have a problem with selecting moving, consider PreviewKeyDown
.
Let us closely examine the problem using KeyCode
and KeyDown
. When the enter key was detected, my dialog would close and I would see the appropriate response.
PreviewKeyDown
and KeyDown
. Take
KeyDown
, and set its event Handled property to true.void dataGridView1_KeyDown(object sender, KeyEventArgs e) { // // Set the key down event has handled. // We call our function ProceedOpen in the PreviewKeyDown event instead. // if (e.KeyCode == Keys.Enter) { e.Handled = true; } }
PreviewKeyDown
exampleHere we stop the KeyDown
from moving the selection. However, before the runtime raises the KeyDown
event, it will raise PreviewKeyDown
.
ProceedOpen
is called on the current cell.void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { // // If the key pressed is enter, then call ProceedOpen. // if (e.KeyCode == Keys.Enter) { ProceedOpen(); } }
PreviewKeyDown
can help your keyboard navigation techniques work well. PreviewKeyDown
can solve selection moved problems in Windows Forms.