Key press event in C#
I want to code a key press event to a text box to accept only alphabets.. any help appreciated.
also, the different validations that I can appl开发者_Go百科y to a normal textbox to not accept anything else than alphabets. even if copy pasted and not just key press//
thanks reggie
You need TextChanged event. You might also want to check MaskedTextBox control.
Use a maskedtextbox control. That will solve your problem.
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx
hook up the event on the textbox:
private void txtBOX_KeyPress(object sender, KeyPressEventArgs e)
{
}
You can use textchange
event, or a mask.
keypress
as mention above is the ugliest way to do it, but the most powerfull.. you can "play" with the input data as you wish.
use these code in your key press event
if(!char.IsLetter(e.keyChar) && e.keyChar!=8)
{
e.handeled=true;
}
These will prevent your text box to accept any thing else alphabets and the backspace button.
精彩评论