Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
I want to write 1234 in textBox0 then add event to textBox0 this event should be activated when i press Enter key.
In propertiy of textBox there are alot of events ,which one will do the job ,i have tried Enter and EnterDrag
it did not work
thanks

What I have tried:

private void EnterPressed(object sender, EventArgs e)
{
    MessageBox.Show("1234");
}

private void DragEnterPressed(object sender, DragEventArgs e)
{
    MessageBox.Show("1234");
}
Posted
Updated 2-Aug-23 23:10pm
v5

To add to what Dave has said, it's a poor idea to do that anyway - several keys have "standard functions" in Windows, and users expect them to do the same thing regardless of the application: TAB either moves to the next input control, or indents text in a document; ENTER submits the form or starts a new line in a document.

When you change that behaviour, you make life difficult for users who have to learn how your app works because it's not the same as everybody else's - and that gets difficult and generally means that users hate your app and don't use it.

But ... doing it the "windows way" is pretty easy: add an "OK" button to your form and handle its Click event from the form designer. (Double click the button and the designer will add it for you ready for you to code.)
Then set the form "Accept Button" property to your button and it will process ENTER for you and call the Click handler.
 
Share this answer
 
You seem to be doing a lot of guessing and no reading of documentation on the TextBox control.

You cannot just name a function to what you want it to do and expect it to work. You have to work with the events you're given by the control, and that stuff is covered in the documentation on it.

What you're trying to do goes against how a Windows application is expected to behave. Enter usually submits an entire form and Tag navigates between fields on a form. Why is this? Accessibility reasons.

For you to do what you want, you'd have to handle the KeyDown event[^] of the textbox and look for the Enter key being pressed. If it's pressed, you can call whatever method you want to handle what happens in that situation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900