c# - How to make Textbox only accept alphabetic characters -
I have an app with a masked textbox control in which I only accept alphabetical values Want to
Ideally, it would behave that any other key would not produce any result than the alphabet keys or immediately give the user feedback about the invalid character.
From
(This code shows how to handle the keydown event to check how to enter characters In this example it is only examining the numerical input, it can modify it so that it works for alphabetical input instead of numeric):
// to determine the boolean flag Used when no other character is entered than one number Land. Private bool nonNumberEntered = false; // Control the keyDown event to determine the type of character recorded in Control. Private Zero Text box1_keydown (Object Sender, System.Windows.Forms.KeyEventArgs E) {// Flag Start False. NonNumberEntered = false; // Define whether a keystroke is a number from the top of the keyboard. If (E. ccode & lt; keys.D0 || e.KeyCode> Key.D9) {// Determine whether the keystroke has a number from the keypad or not (if E. ccode & lieutenant; keys no. Pad 0 || E. cacode> Keys. Numpad 9) {// Determine whether keystroke is a backspace or not. If (E.kcode! = Key.back) {// A non-numerical keystroke was pressed. // Set the flag to the right and evaluate in KeyPress event NonNumberEntered = True; }}} // If the shift key was pressed, this number is not if (Control.ModifierKeys == Keys.Shift) {nonNumberEntered = true; }} // This event occurs after the keydown event and can be used to prevent the // character from entering the control. Private Zero Text Box 1_Campress (Object Sender, System.Windows.Form.PresspressEventErgusE) {// Check the Flag Setting in the ChildDown Event if (nonNumberEntered == true) {// stop the character from being recorded in the control because This is non-numerical. E.Handled = True; }}
Comments
Post a Comment