private void TextBoxKeyPress(object sender, KeyPressEventArgs e) { //================48代表0,57代表9,8代表退格删除,46代表小数点 if ((e.KeyChar <= 48 || e.KeyChar >= 57) && (e.KeyChar != 8) && (e.KeyChar != 46)) e.Handled = true; }View Code 2.在需要调用的地方调用
private void Frm_Load(object sender, EventArgs e) { //校验只能输入数字 this.txtCount.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextBoxKeyPress); }View Code