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   }
原文地址:https://www.cnblogs.com/kay/p/1255779.html