c# - Detect Key in KeyUp event -


i have textbox on form i'm trying detect keys user types in. textbox multilined wordwrap on. don't want user press enter key (as want text entered on 1 line, wrapped) used following code:

private void txtplain_keypress(object sender, keypresseventargs e) {     if (e.keychar == (char)13) {         messagebox.show("enter keys not allowed");         e.keychar = (char)0;     } } 

this worked fine in tests, when tested ctrl+enter didn't work i'm not sure how detect control key. googling found need use keyup/down events have following code:

private void txtplain_keyup(object sender, keyeventargs e) {     //if (e.keydata == (keys.control | keys.enter)) {      if (e.keycode == keys.enter || (e.keycode == keys.enter && e.control)) {                     messagebox.show("enter keys not allowed:");         //e.keyvalue = keys.none;     } } 

the first commented out line didn't work reason if explain why useful.

the problem keyup/down event don't know how remove enter key text - unlike keypress event when can set keychar zero. event captures both enter , ctrl+enter keys, cursor still goes next line in textbox.

thanks on this.

hmm, there's no reason disallow enter key handling keydown or keyup events. can set acceptsreturn property of textbox control false. prevent multiline textbox responding press of enter key.

of course, doesn't solve problem of ctrl+enter. in fact, that's expected way create new line when acceptsreturn property set false. solve that, will need handle 1 of keyboard events , prevent control receiving input.

keydown place start. want filter out any keyboard events include keys.enter flag. catch them no matter other modifier key might combined with. then, once you've found enter keypress, want set e.handled property true in order prevent being passed on control.

but unfortunately, we're not quite done yet. textbox control tries handle keys internally, , you're not going able override in key event handler method. need tell control not interpret particular key input key. there 2 primary ways of doing this. first (and recommended way) inherit base textbox class create own custom control, , override protected isinputkey method. second (somewhat simpler) way handle previewkeydown event, , set isinputkey property false.

sample code:

private void txtplain_previewkeydown(object sender, previewkeydowneventargs e) {     // check if keycode value has keys.enter flag set     if ((e.keycode & keys.enter) == keys.enter)     {         // set isinputkey property false         e.isinputkey = false;     } }  private void txtplain_keydown(object sender, keyeventargs e) {     // check if keycode value has keys.enter flag set     if ((e.keycode & keys.enter) == keys.enter)     {                     // show user message         messagebox.show("enter keys not allowed in textbox.");          // prevent key event being passed on control         e.handled = true;     } } 

and, though assume testing purposes only, want take messagebox call out of there production code. find way alert user input not allowed, such short beep sound , errorprovider component placed next textbox. showing message box jarring, , not user-friendly. see my answer here other hints , tips.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -