c# - How to stop default function of keyboard key? -
i made textbox
, bind space check if string in textbox1
, richtextbox1
same:
if (e.keycode == keys.space) { if (richtextbox1.text.contains(textbox1.text)) { richtextbox1.text = richtextbox1.text.replace(textbox1.text + " ", ""); wpm++; textbox1.text = ""; } }
so want when press space not write space in textbox1
this answer adapted msdn example here
// boolean flag used determine when character other space entered private bool spaceentered = false; // handle keydown event determine type of character entered control. private void textbox1_keydown(object sender, system.windows.forms.keyeventargs e) { spaceentered = e.keycode == keys.space; } // event occurs after keydown event , can used prevent // characters entering control. private void textbox1_keypress(object sender, system.windows.forms.keypresseventargs e) { // check flag being set in keydown event. if (spaceentered == true) { // stop character being entered control since e.handled = true; } }
Comments
Post a Comment