protected override void WndProc(ref System.Windows.Forms.Message message) { const int WM_GETDLGCODE = 0x87; const int VK_ENTER = 0xd; const int VK_ESCAPE = 0x1b; const int DLGC_WANTALLKEYS = 0x0004; switch (message.Msg) { case WM_GETDLGCODE: switch ((int)message.WParam) { case VK_ENTER: case VK_ESCAPE: message.Result = (IntPtr)DLGC_WANTALLKEYS; base.ProcessKeyMessage(ref message); break; default: base.WndProc(ref message); break; } break; default: base.WndProc(ref message); break; } }
// Customized TextBox Column public class DataGridViewCustomTextBoxColumn : DataGridViewTextBoxColumn { public DataGridViewCustomTextBoxColumn() { this.CellTemplate = new DataGridViewCustomTextBoxCell(); } } // Customized TextBox Cell public class DataGridViewCustomTextBoxCell : DataGridViewTextBoxCell { public override Type EditType { get { return typeof(DataGridViewCustomTextBoxEditingControl); } } } // Customized TextBox Editing Control public class DataGridViewCustomTextBoxEditingControl : DataGridViewTextBoxEditingControl { protected override void WndProc(ref System.Windows.Forms.Message message) { const int WM_GETDLGCODE = 0x87; const int VK_ENTER = 0xd; const int VK_ESCAPE = 0x1b; const int DLGC_WANTALLKEYS = 0x0004; switch (message.Msg) { case WM_GETDLGCODE: switch ((int)message.WParam) { case VK_ENTER: case VK_ESCAPE: message.Result = (IntPtr)DLGC_WANTALLKEYS; base.ProcessKeyMessage(ref message); break; default: base.WndProc(ref message); break; } break; default: base.WndProc(ref message); break; } } }
Private Sub txtPIDSearch_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPIDSearch.KeyUp Try If e.KeyValue = Keys.Enter Then 'do something End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub
If only it were that simple!
On ESRI Property Pages, KeyDown and KeyUp never get fired for Enter and Escape. The window is gone before that. You have to get down to the WinAPI level to trap the WM_GETDLGCODE message, set its result to DLGC_WANTALLKEYS, and then send the message on to the appropriate handler (I used ProcessKeyMessage). If you don't set its result, the handler won't get it because Property Pages by default make the system handle dialog keys (Escape/Enter).
I think it's a little different with Toolbars, but I will definitely keep your solution in mind if I have to do something similar. Thanks!