textbox basic question

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,
 
S

Scott M.

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
 
G

Guest

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

Thanks,
LW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top