方法一:
private void txtEmployCode_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = true; if (e.KeyChar >= '0' && e.KeyChar <= '9') { e.Handled = false; } }
方法二:
public static void NumberAccpter(object sender, KeyPressEventArgs e, bool point) { int keyChar = e.KeyChar; if ((((keyChar >= 0x30) && (keyChar <= 0x39)) || (keyChar == 8)) || (keyChar == 0x2e)) { if (!(point || (keyChar != 0x2e))) { e.Handled = true; } else { string text = ""; if ((sender != null) && (sender is TextBox)) { text = ((TextBox)sender).Text; } if ((text == "") && (keyChar == 0x2e)) { e.Handled = true; } else if ((text.IndexOf(".") >= 0) && (keyChar == 0x2e)) { e.Handled = true; } else { e.Handled = false; } } } else { e.Handled = true; } }