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