<summary>///price and quantity input control (only one decimal point can be entered, only two digits can be input, first digit cannot be a decimal point)///</summary>///<param name= "E" > KeyPressEventArgs e</param>///<param name= "TextBox" >TextBox</param> public static void Keypressxs ( KeyPressEventArgs E, textbox textbox {if (char). Isnumber (E.keychar) | | E.keychar = = '. ' | | E.keychar = = (char) keys.back) {e.handled = false;//Let the action take effect int j = 0; int k = 0; bool flag = false; if (textBox.Text.Lengt H = 0) {if (E.keychar = = '. ') {e.handled = true;//Let Operation fail}} for (int i = 0; i < textBox.Text.Length i++) {if (textbox.text[i] = = '. ') {j + +; flag = true;} if (flag) {if (char). Isnumber (Textbox.text[i]) && E.keychar!= (char) keys.back) {k++;}} if (J >= 1) {if (E.keychar = = '. ') {e.handled = true;//Let Operation fail}} if (k = = 2) {if (char). Isnumber (Textbox.text[i]) && E.keychar!= (char) keys.back) {if (Textbox.text.length-textbox.selectionstart & Lt 3) {if (Textbox.selectedtext!= textbox.text) {e.handled = true;////Let actionInvalid}}}}/else {e.handled = true;}}
There's a problem.
First enter the integer part and then the whole part of the deletion, in disguise let the decimal point to the first place
In the case of a two-bit decimal, you cannot change a two-bit number if selected
Netizen Ymwcwee has corrected please refer to his code
<p><textarea cols= "rows=" name= "code" class= "C-sharp" >///<summary>
Http://blog.csdn.net/jzz870519/archive/2010/08/08/5796876.aspx, because I always failed to reply to the code, so I opened it myself ...
View plain Copy to clipboard print? Private void textbox1_keypress (object sender, keypresseventargs e) { if (char. Isnumber (E.keychar) | | e.KeyChar == '. ' | | e.KeyChar == (char) keys.back | | e.KeyChar == (char) keys.delete) { e.Handled = false; //let the operation take effect int j = 0; //record number of decimal points int k = 0; //Record decimal places int dotloc = -1; //Record decimal point position bool flag = false; // If there is a decimal point, let flag value true // //Remove First is 0 of the judgment, because we do not know the user's intention, maybe Ta next time to enter a number before the decimal point. /* if (textbox1.text.length == 0) { if (e.keychar == '.') { e.handled = true; } } */ // for (int i = 0; i < textbox1.text.length; i++) { if (textbox1.text[i] == '. ') { j++; flag = true; &NBSP;&NBSP;&NBSP;&Nbsp; dotloc = i; } if (flag) { if (char. Isnumber (Textbox1.text[i]) && e.KeyChar != (char) keys.back && e. keychar != (char) keys.delete) { k++; } } if (j >= 1) { if (E. keychar == '. ') { if (TextBox1.SelectedText.IndexOf ('. ') == -1) e.Handled = true;   //enter "." and the selection does not have "." Operation Failure } } if (!flag) // This controls whether adding a decimal point when there is no decimal point satisfies two decimal places { if (e.keychar == '. ') { if (textbox1.text.length - textbox1.selectionstart - textbox1.selectedtext.length > 2) //the condition also can be instead of " TextBox1.Text.Substring (Textbox1.selectionstart). Length-textbox1.selectionlength>2 " e.handled = true; } } if (K&NBSP;==&NBSP;2) { if . (Textbox1.selectionstart > textbox1.text.indexof ('. ') && textBox1.SelectedText.Length == 0 && e.KeyChar! = (char) keys.delete && e.keychar!= (char) keys.back) //If you already have two decimal places, the cursor is to the right of the decimal point, e.Handled = true; } } } else { e.Handled = true; } }
</textarea></p>