textbox basic question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I have a basic question on using a textbox in my windows application.
I am using C#, VS .NET 2005.

I have a single line textbox where the user enters text. When the user is
done, I want the user to enter <return> to indicate that he is done with the
data entry.
I cannot find a way to capture that event. Most examples I have seen require
the user to press a button on the form which is something I would like to
avoid.

How do I do that?

Thanks,
 
First, let me say that I think your approach to the way the form would be
used is counter-intuitive to how people expect to use forms and do data
entry. The reason most examples show a button that is clicked or the user
simply hitting the TAB key or just clicking into the next data entry field
is because those are the most common ways to use forms.

Having said that, your textbox exposes a keydown and a keypress event, in
these event handlers you can simply test for the ENTER key with something
like this:

private void keypressed(Object o, KeyPressEventArgs e)
{
// The keypressed method uses the KeyChar property to check
// whether the ENTER key is pressed.

// If the ENTER key is pressed, do something
if (e.KeyChar == (char)Keys.Return)
{
//do something here
}
}

See:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.keypress(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar(VS.80).aspx

For more details.

-Scott
 
Scott -
Thanks for the info. Thinking about it some more I have added a button that
the user presses.

Thanks,
LW
 
Back
Top