使用TextBox的KeyPress事件
/// <summary> /// 去除非十进制和Backspace和Enter /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar!=8 && !char.IsDigit(e.KeyChar))&&e.KeyChar!=13) { MessageBox.Show("请输入数字","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information); e.Handled = true; } }