//control controls Enter a positive number (including decimals) Private voidTxt_weight_keypress (Objectsender, KeyPressEventArgs e) { //The first step: Determine whether the input is a digital--char. Isnumber (E.keychar)//if it is a number, you can enter (E. Handled = false;)//if it is not a number, determine if it is a decimal point if(Char. Isnumber (E.keychar)) {e.handled=false; } Else { //determines whether the input is a decimal point, or a period in the Chinese state, or a backspace//if it is a decimal point, the loop determines whether each character is a decimal point, if there is no input if there is no input allowed//If the BACKSPACE key, allow input--if (E.keychar = = ' \b ')//if it is not a decimal point and is not a backspace key, the input is not allowed if(E.keychar = = Convert.tochar (". ") || E.keychar = = Convert.tochar (".")) { intI_d =0; for(inti =0; i < txt_Weight.Text.Length; i++) { if(Txt_Weight.Text.Substring (I,1) ==".") {e.handled=true; I_d++; return; } } if(I_d = =0) {
E.keychar = Convert.tochar ("."); /Set the key input value to "." E.handled=false; } } Else if(E.keychar = ='\b') {e.handled=false; } Else{e.handled=true; } } }
c#--control text box can only enter integers (including decimals)