定义文本框的KeyPress事件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private void txtDateEmployed_KeyPress(object sender, KeyPressEventArgs e) 2 { 3 //获取按键的ASCII值 4 int ascii = (int)e.KeyChar; 5 if (ascii >= 48 && ascii <= 57 ) 6 { 7 //允许输入 8 e.Handled = false; 9 } 10 else 11 { 12 e.Handled = true; 13 } 14 }