Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enter key pressed event handler

I want to capture the text from the textbox when enter key is hit. I am using WPF/visual studio 2010/.NET 4. I dont know what event handler to be used in the tag ? I also want to do the same for maskedtextbox.

like image 384
zack Avatar asked Sep 20 '10 14:09

zack


People also ask

How do you check if the Enter key is pressed?

To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox'). keypress(function(event){ var keycode = (event.

How do you trigger button click on enter?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.

How do you submit a form when Enter key is pressed?

To submit the form using 'Enter' button, we will use jQuery keypress() method and to check the 'Enter' button is pressed or not, we will use 'Enter' button key code value. Explanation: We use the jQuery event. which to check the keycode on the keypress.


2 Answers

Either KeyDown or KeyUp.

TextBox tb = new TextBox(); tb.KeyDown += new KeyEventHandler(tb_KeyDown);  static void tb_KeyDown(object sender, KeyEventArgs e) {     if (e.KeyCode == Keys.Enter)     {         //enter key is down     } } 
like image 160
tafa Avatar answered Sep 29 '22 05:09

tafa


You can also use PreviewKeyDown in WPF:

<TextBox PreviewKeyDown="EnterClicked" /> 

or in C#:

myTextBox.PreviewKeyDown += EnterClicked; 

And then in the attached class:

void EnterClicked(object sender, KeyEventArgs e) {     if(e.Key == Key.Return) {         DoSomething();         e.Handled = true;     } } 
like image 26
Stuart Avatar answered Sep 29 '22 05:09

Stuart