The textbox box under winform control cannot contain letters [Z]

Source: Internet
Author: User
Method 1:  Private   Void Tbox_keypress ( Object  Sender, keypresseventargs e ){  If (E. keychar = 0x20 ) E. keychar = ( Char ) 0 ; //  Disable Space key              If (E. keychar = 0x2d ) & (Textbox) sender). Text. Length =0 )) Return ; //  Process negative numbers              If (E. keychar> 0x20  ){  Try  {  Double . Parse (textbox) sender). Text + E. keychar. tostring ());}  Catch  {E. keychar = (Char ) 0 ; //  Handle invalid characters  }}} Method 2:  Private   Void Textbox_keypress ( Object  Sender, keypresseventargs e ){  If (E. keychar! = 8 &&! Char. isdigit (E. keychar) {e. Handled = True ;} Or  Private   Void Textbox_keypress ( Object  Sender, keypresseventargs e ){  If (E. keychar! = '  \ B  ' &&! Char. isdigit (E. keychar) {e. Handled = True  ;} Method 3:  Private   Void Textbox1_keypress (Object  Sender, system. Windows. Forms. keypresseventargs e ){  If (E. keychar! = '  \ B  ' ) //  This is the allowed backspace key  {  If (E. keychar < '  0  ' ) | (E. keychar> '  9 ' )) //  This is a 0-9 number.  {E. Handled = True  ; }}} Method 4:  Private   Void Textbox1_validating ( Object  Sender, canceleventargs e ){  Const   String Pattern = @"  ^ \ D + \.? \ D + $  " ;  String Content = (Textbox) sender). text;  If (! (RegEx. ismatch (content, pattern) {errorprovider1.seterror (Control) sender,  "  Only numbers are allowed!  "  ); E. Cancel = True  ;}  Else  Errorprovider1.seterror (Control) sender,  Null );} Method 5:  Private   Void Textbox1_keypress ( Object  Sender, system. Windows. Forms. keypresseventargs e ){  If (E. keychar = '  .  ' && This . Textbox1.text. indexof ( "  .  " )! =- 1 ) {E. Handled = True  ;}  If (! (E. keychar> = 48 & Amp; E. keychar <= 57 ) | E. keychar = '  .  ' | E. keychar = 8  ) {E. Handled = True  ;} Method 6:  Private   Void Tbx_lsregcapital_keypress ( Object  Sender, keypresseventargs e ){  If (! Char. isnumber (E. keychar )&&! Char. ispunctuation (E. keychar )&&! Char. iscontrol (E. keychar) {e. Handled = True ; //  Remove inappropriate characters  }  Else   If  (Char. ispunctuation (E. keychar )){ If (E. keychar! = '  .  ' | This . Textbox1.text. Length = 0 ) //  Decimal point  {E. Handled = True  ;}  If (Textbox1.text. lastindexof ( '  . ' )! =- 1  ) {E. Handled = True  ; }}} Method 7: ASCII code solution ,{  If (E. keychar <= 48 | E. keychar> = 57 ) & (E. keychar! = 8 ) & (E. keychar! = 46  ) E. Handled = True ; ====================== 48 represents 0, 57 represents 9, 8 represents space, 46 represents the decimal point} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.