Jump to content

Getting keypress Enter

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Chan

Chan

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 204 posts
How do I capture when the user presses the enter key in a certain text box?

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Capture the "keypress" event of your control and add this code:

if (e.KeyChar == (char)13)
{
    // Code
    ......................
}


#3
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
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_*

Guest_Jordan_*
  • Guests
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

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
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
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.

#6
balasankar.segi

balasankar.segi

    Newbie

  • Members
  • Pip
  • 1 posts

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

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
mokszyk

mokszyk

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
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 :)