c# - How to change the focus inside a form and make the new control deal with the key pressed? -
i have form search box , datagridview results. need search box focus whenever press char or number key, , process key. so, instance, if focus on datagrid, , press char, it'll go search box. (additionally, whenever or down arrows pressed in form, focus go datagrid, got no problem that.)
now, code below work charm chars. when numbers, though, can't same way (as e.keycode.tostring()
not give me number, "d0" or "numpad0").
private void form_keydown(object sender, keyeventargs e) { if (!this.tbxsearch.focused) { if ((e.keycode >= keys.a) && (e.keycode <= keys.z)) { this.tbxsearch.focus(); this.tbxsearch.text = (e.shift) ? e.keycode.tostring() : e.keycode.tostring().tolower(); this.tbxsearch.selectionstart = this.tbxsearch.text.length; } } }
of course, might same way, , last char in "e.keycode.tostring()
", doesn't seem alright me. mean, there must simpler way of changing focus , making control deal key pressed.
i've tried making in previewkeydown
event, won't triggered form (even keypreview
property set true
):
private void form_previewkeydown(object sender, previewkeydowneventargs e) { if ((e.keycode == keys.down) || (e.keycode == keys.up)) this.dgvresults.focus(); else this.tbxsearch.focus(); }
so left wondering: how can change focus on form , let new control deal key?
you can check if key digit this:
private keys[] numerickeys = { keys.d1, keys.d2, keys.d3, keys.d4, keys.d5, keys.d6, keys.d7, keys.d8, keys.d9, keys.d0, keys.numpad0, keys.numpad1, keys.numpad2, keys.numpad3, keys.numpad4, keys.numpad5, keys.numpad6, keys.numpad7, keys.numpad8, keys.numpad9 }; ... bool isdigit = numerickeys.contains(e.keycode);
or this:
bool isdigit = char.isdigit((char)e.keycode);
or:
bool isletterordigit = char.isdigit((char)e.keycode);
Comments
Post a Comment