WinForms中只能输入数字的文本框

只需要在文本框的keyPress事件中写如下代码即可:

 

1    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
2   {
3       //只能是0~9之间的数字 并支持删除键
4        if (!((e.KeyChar >= '0' && e.KeyChar <= '9'|| e.KeyChar == ''))
5       {
6           e.Handled = true;
7       }
8             
9   }
posted @ 2008-07-29 17:33  樊凯  阅读(654)  评论(0编辑  收藏  举报