//方法一 if (e.KeyChar == 0x20) e.KeyChar = (char)0; //禁止空格键 if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; //处理负数 if (e.KeyChar > 0x20) { try { double.Parse(((TextBox)sender).Text + e.KeyChar.ToString()); } catch { e.KeyChar = (char)0; //处理非法字符 } } //方法二 //if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) //{ // e.Handled = true; //} //方法三 //if (e.KeyChar != '' && !Char.IsDigit(e.KeyChar)) //{ // e.Handled = true; //} //方法四 //if (e.KeyChar != '')//这是允许输入退格键 //{ // if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字 // { // e.Handled = true; // } //} //方法五 //if ((e.KeyChar <= 48 || e.KeyChar >= 57) && (e.KeyChar != 8) && (e.KeyChar != 46)) //{ // e.Handled = true; //}