Detecting the enter key
-
Hi all, I have a combo box and a text box on a form. cmb1 and txt1 for arguments sake. The tab order means that when tab is pressed focus moves from cmb1 to txt1. What i want to do is allow for the user to press the enter key and simulate the tab key being pressed, i.e. focus moves to the next control on the tab order. Any ideas? Cheers Kev
-
Hi all, I have a combo box and a text box on a form. cmb1 and txt1 for arguments sake. The tab order means that when tab is pressed focus moves from cmb1 to txt1. What i want to do is allow for the user to press the enter key and simulate the tab key being pressed, i.e. focus moves to the next control on the tab order. Any ideas? Cheers Kev
You could check for the Enter key in the combo's KeyPress event, then transfer focus to the next in the tab order.
private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if ( e.KeyChar == (char)13 ) { this.GetNextControl(this.ActiveControl, true).Focus(); } }
Roger Stewart "I Owe, I Owe, it's off to work I go..."
-
You could check for the Enter key in the combo's KeyPress event, then transfer focus to the next in the tab order.
private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if ( e.KeyChar == (char)13 ) { this.GetNextControl(this.ActiveControl, true).Focus(); } }
Roger Stewart "I Owe, I Owe, it's off to work I go..."
You should actually use
KeyCode.Enter
. While this will probably always be character code 13, without optimization (based on the compiler)(char)13
may require additional instructions for the cast while usingKeyCode.Enter
will always compile to 13 without a cast. Besides, it's best to avoid "magic numbers" and makes for more elegant code. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
You should actually use
KeyCode.Enter
. While this will probably always be character code 13, without optimization (based on the compiler)(char)13
may require additional instructions for the cast while usingKeyCode.Enter
will always compile to 13 without a cast. Besides, it's best to avoid "magic numbers" and makes for more elegant code. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]I took this (the char 13) straight out of MSDN Library for the KeyPressEventArgs.KeyChar Property[^]. You might want to pass your suggestion on to MSDN Lib guys :-D:rose: Roger Stewart "I Owe, I Owe, it's off to work I go..."
-
I took this (the char 13) straight out of MSDN Library for the KeyPressEventArgs.KeyChar Property[^]. You might want to pass your suggestion on to MSDN Lib guys :-D:rose: Roger Stewart "I Owe, I Owe, it's off to work I go..."