博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net(c#) winform文本框只能输入数字,不能其他非法字符
阅读量:6957 次
发布时间:2019-06-27

本文共 1763 字,大约阅读时间需要 5 分钟。

1 private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 2 { 3     //阻止从键盘输入键 4     e.Handled = true; 5     if(e.KeyChar>='0' && e.KeyChar <='9') 6     { 7         e.Handled = false; 8     } 9 10 }11 或者12 private void tbID_KeyPress(object sender, KeyPressEventArgs e)13         {14             if (!((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == ' '))//不输入输入除了数字之外的所有非法字符的判断15             {16                 e.Handled = true;17             }18         }19 20 21 多条件的:22 23 private void TxtUser_KeyPress(object sender, KeyPressEventArgs e)24         {25             //阻止从键盘输入键26            e.Handled = true;27 28             if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == (char)8))29             {30 31                 if ((e.KeyChar == (char)8)) { e.Handled = false; return; }32                 else33                 {34                     int len = TxtUser.Text.Length;35                     if (len < 5)36                     {37                         if (len == 0 && e.KeyChar != '0')38                         {39                             e.Handled = false; return;40                         }41                         else if(len == 0)42                         {43                             MessageBox.Show("编号不能以0开头!"); return;44                         }45                         e.Handled = false; return;46                     }47                     else48                     {49                         MessageBox.Show("编号最多只能输入5位数字!");50                     }51                 }52             }53             else54             {55                 MessageBox.Show("编号只能输入数字!");56             }57            58 59         }
View Code

 

转载于:https://www.cnblogs.com/zxbzl/p/3182295.html

你可能感兴趣的文章
Elementary Methods in Number Theory Exercise 1.5.11
查看>>
化一阶线性方程为恰当方程
查看>>
服务器使用ssh秘钥登录并禁止密码登录
查看>>
django基础知识~forms钩子
查看>>
javascript预解释中的机制
查看>>
正则表达式pattern的匹配格式
查看>>
JDOM
查看>>
MySQL 最基本的SQL语法/语句
查看>>
洛谷 P2661 信息传递 Label:并查集||强联通分量
查看>>
Linux下搭建ftp服务器(转载)
查看>>
hadoop之 HDFS-Hadoop存档
查看>>
搭建时间服务器
查看>>
php 多进程 父进程的阻塞与非阻塞
查看>>
asp.net core mvc ActionFilterAttribute 获取自动定义Attribute
查看>>
sealed、new、virtual、abstract与override 趣解
查看>>
[bzoj 4650][NOI 2016]优秀的拆分
查看>>
crossplatform---Nodejs in Visual Studio Code 08.IIS
查看>>
OGNL表达式入门
查看>>
Java 诊断工具 Arthas 教程学习笔记
查看>>
bootstrap 2.3版与3.0版的使用区别
查看>>