How do I capture when the user presses the enter key in a certain text box?
Getting keypress Enter
Started by Chan, Sep 05 2006 04:53 PM
6 replies to this topic
#1
Posted 05 September 2006 - 04:53 PM
|
|
|
#2
Guest_Jordan_*
Posted 07 September 2006 - 04:08 PM
Guest_Jordan_*
Capture the "keypress" event of your control and add this code:
if (e.KeyChar == (char)13)
{
// Code
......................
}
#3
Posted 07 September 2006 - 05:46 PM
Jordan's got it right. Though I'd recommend using the System.Windows.Forms.Keys enum instead of 13. Just a style guidance, though - functionally, it should be the same.
#4
Guest_Jordan_*
Posted 08 September 2006 - 05:27 AM
Guest_Jordan_*
Actually, that is the way I prefer it too brackett but I couldn't get it to work in C#. I looked through all of the properties of e in the above code and nothing would would convert. So, for instance
will not compile.
in managed C++ 2.0 I know there is a keycode property of KeyEventArgs so code would look like this:
Not sure if this is a bug or what.....
if (e.keychar == system.windows.forms.keys.enter) {}
will not compile.
in managed C++ 2.0 I know there is a keycode property of KeyEventArgs so code would look like this:
if (e->KeyCode == System::Windows::Forms::Keys::Enter)
Not sure if this is a bug or what.....
#5
Posted 08 September 2006 - 12:16 PM
Quick browse of MSDN2 shows that KeyDown and KeyUp use KeyEventArgs, which includes the KeyCode property. KeyPress uses KeyPressEventArgs, which does not have a KeyCode property (only KeyChar).
Unfortunately, it also states that without overriding the IsInputKey method of the control, Enter will be handled by the control directly and not raise the KeyDown/KeyUp events. I'm unclear on whether KeyPress is raised or not.
Yeah - I think there's a design mistake in there somewhere. You'd think KeyPressEventArgs would inherit from KeyEventArgs (adding the Handled flag). But, as it stands, I guess casting to char is the easiest way to do it.
I'd suspect System.Windows.Forms.Keys.Enter is 13, so you could cast that to char. At least it'd give some contextual information on what you're trapping.
Unfortunately, it also states that without overriding the IsInputKey method of the control, Enter will be handled by the control directly and not raise the KeyDown/KeyUp events. I'm unclear on whether KeyPress is raised or not.
Yeah - I think there's a design mistake in there somewhere. You'd think KeyPressEventArgs would inherit from KeyEventArgs (adding the Handled flag). But, as it stands, I guess casting to char is the easiest way to do it.
I'd suspect System.Windows.Forms.Keys.Enter is 13, so you could cast that to char. At least it'd give some contextual information on what you're trapping.
#6
Posted 22 July 2010 - 01:57 AM
Jordan said:
Actually, that is the way I prefer it too brackett but I couldn't get it to work in C#. I looked through all of the properties of e in the above code and nothing would would convert. So, for instance
will not compile.
in managed C++ 2.0 I know there is a keycode property of KeyEventArgs so code would look like this:
Not sure if this is a bug or what.....
if (e.keychar == system.windows.forms.keys.enter) {}
will not compile.
in managed C++ 2.0 I know there is a keycode property of KeyEventArgs so code would look like this:
if (e->KeyCode == System::Windows::Forms::Keys::Enter)
Not sure if this is a bug or what.....
try this man
if (e.keychar == (char)system.windows.forms.keys.enter) {}
#7
Posted 27 July 2010 - 12:20 AM
this works btw.
private void lblist_KeyDown(object sender, KeyEventArgs e) //it could be a textbox instead listbox :)
{
if(e.KeyData == Keys.Enter)
}
THink positive :)


Sign In
Create Account


Back to top









