C # Set restrictions on the textbox like quantity, price, amount, etc., users can only enter numbers or decimals

Source: Internet
Author: User

1        #regionSet the TextBox control style and restrictions such as quantity (the method to invoke is to override or call the Shieldnumbertextboxotherkeys function directly)2 3         /// <summary>4         ///masking other strings of a digital textbox5         /// </summary>6         /// <param name= "Sender" ></param>7         /// <param name= "E" ></param>8          Public Virtual voidTboxnumbers_keydown (Objectsender, KeyEventArgs e)9         {TenE.suppresskeypress =true; One  A             Switch(E.keycode) -             { -                  Casekeys.d0: the                  Casekeys.d1: -                  CaseKEYS.D2: -                  Casekeys.d3: -                  Casekeys.d4: +                  Casekeys.d5: -                  CaseKeys.d6: +                  CaseKeys.d7: A                  CaseKeys.d8: at                  Casekeys.d9: -                  Casekeys.numpad0: -                  CaseKeys.numpad1: -                  Casekeys.numpad2: -                  Casekeys.numpad3: -                  Casekeys.numpad4: in                  CaseKEYS.NUMPAD5: -                  CaseKeys.numpad6: to                  CaseKeys.numpad7: +                  CaseKeys.numpad8: -                  Casekeys.numpad9: the                  CaseKeys.back: *                  CaseKeys.oemperiod: $                  CaseKeys.delete:Panax Notoginseng                  CaseKeys.decimal: -E.suppresskeypress =false; the                      Break; +                 default: A                      Break; the             } +         } -  $          Public Virtual voidTboxnumbers_keypress (Objectsender, KeyPressEventArgs e) $         { -TextBox Tbox = Sender asTextBox; -             Charc =E.keychar; the  -             if(C.tostring (). Equals ("."))Wuyi             { the                 if(TBox.Text.Length <=0) -e.handled =true;//the decimal point cannot be the first digit Wu                 Else -                 { About                     floatF; $                     floatOldf; -                     BOOLB1 =false, b2 =false; -B1 =float. TryParse (Tbox.text, outoldf); -B2 =float. TryParse (Tbox.text + e.keychar.tostring (), outf); A                     if(B2 = =false) +                     { the                         if(B1 = =true) -e.handled =true; $                         Else thee.handled =false; the                     } the                 } the             } -         } in  the         /// <summary> the         ///masking other strings of a digital textbox About         /// </summary> the         /// <param name= "Tbox" >textbox to block</param> the          Public Virtual voidShieldnumbertextboxotherkeys (TextBox tbox) the         { +Tbox. ImeMode =imemode.disable; -Tbox. KeyDown + =Tboxnumbers_keydown; theTbox. KeyPress + =tboxnumbers_keypress;Bayi         } the  the         #endregion

C # Set restrictions on the textbox like quantity, price, amount, etc., users can only enter numbers or decimals

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.