WinForm 限制TextBox只能输入数字和小数点
/// <summary> /// 限制TextBox只能输入数字和小数点 /// 一般在TextBox的KeyPress中调用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public static void TextBox_KeyPress(object sender, KeyPressEventArgs e) { if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31)) { if (e.KeyChar == '.') { if (((TextBox)sender).Text.Trim().IndexOf('.') > -1) e.Handled = true; } else e.Handled = true; } else { if (e.KeyChar <= 31) { e.Handled = false; } } }