public static void Key_Press(KeyPressEventArgs e, TextBox txt) { //if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar)) //{ // e.Handled = true; //} if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46) e.Handled = true; //小数点的处理。 if (e.KeyChar == 46 ) // decimal { IF (txt.Text.Length == 0 ) e.Handled = to true ; // decimal point is not in the first place the else { a float F; a float oldf; BOOL B1 = to false , B2 = to false ; B1 = a float .TryParse (txt.Text, OUT oldf); B2 = a float.TryParse(txt.Text + e.KeyChar.ToString(), out f); if (b2 == false) { if (b1 == true) e.Handled = true; else e.Handled = false; } } } }