winforms - C# keyup event issue -
i use keyup event add functionality pressing enter cursor move next control. event works typed in textbox , press enter cursor move next control letters typed in previous control clear. did not add clear function anywhere in code. here code:
private void nametextbox_keyup(object sender, keyeventargs e) { if(e.keycode == keys.enter) { this.selectnextcontrol(nametextbox, true, true, true, true); } } private void emailtextbox_keyup(object sender, keyeventargs e) { if (e.keycode == keys.enter) { this.selectnextcontrol(agetextbox, true, true, true, true); } }
set e.handled
flag true
prevent default key actions suspect adding new line control , hiding text:
if(e.keycode == keys.enter) { this.selectnextcontrol(nametextbox, true, true, true, true); e.handled = true; }
you need override processcmdkey
in order prevent new lines being entered , hiding text. (alternatively can set textboxes single line if works you). otherwise, add window logic:
protected override bool processcmdkey(ref message msg, keys keydata) { if (keydata == keys.enter) return true; return base.processcmdkey(ref msg, keydata); }
returning true flag default command should not performed. affect controls in window however, meaning need manually add newline functionality if needed.
can suggest use same handler each of key events, unless have different add each. can passing sender control
instead of each control manually:
private void control_keyup(object sender, keyeventargs e) { if(e.keycode == keys.enter) if(sender control) this.selectnextcontrol((control)sender, true, true, true, true); }
Comments
Post a Comment