C# 限制textbox只能输入数字 C#限制textbox只能输入数字
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
在C#编程中,TextBox控件是用于用户输入文本的常用元素。然而,在某些场景下,我们可能希望限制用户只能在TextBox中输入数字,避免输入其他字符,如字母、符号等。这通常在处理诸如电话号码、身份证号、年龄等数值输入时非常有用。以下是如何在C#中实现这一功能的详细步骤: 我们可以利用TextBox的`TextChanged`事件来实时检测用户输入的变化。当用户在TextBox中输入时,触发这个事件,然后我们检查新输入的字符是否为数字。如果输入非法,我们就将其回退,保持TextBox内只含有数字。 ```csharp private void textBox1_TextChanged(object sender, EventArgs e) { TextBox textBox = (TextBox)sender; string text = textBox.Text; // 仅保留最后一个数字字符,删除所有非数字字符 text = new string(text.Where(c => Char.IsDigit(c)).ToArray()); // 如果当前文本与过滤后的文本不同,更新文本框内容 if (text != textBox.Text) textBox.Text = text; } ``` 这段代码会监听TextBox的`TextChanged`事件,并使用LINQ表达式筛选出字符串中的数字字符,创建新的字符串。如果新字符串与原字符串不同,则将TextBox的文本替换为新字符串,从而实现只允许输入数字的功能。 除此之外,还可以使用`KeyPress`事件来阻止非法字符的输入,这样可以更早地拦截非法字符,防止其进入TextBox。在`KeyPress`事件中,我们检查按键的`KeyChar`属性,判断它是否为数字键: ```csharp private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) { e.Handled = true; } } ``` 这里,我们检查`e.KeyChar`是否为数字字符或控制字符(如Backspace键)。如果不是,我们将`e.Handled`设置为`true`,阻止该字符被输入。 在实际开发中,我们还可以结合这两种方法,确保用户在多种方式下都无法输入非数字字符。同时,考虑到用户可能会复制粘贴文本,也可以在`Paste`事件中进行同样的检查和处理。 C#中限制TextBox只输入数字主要通过监听文本改变或按键事件,并对输入进行验证和过滤来实现。这种方式既提高了用户体验,也确保了数据的准确性。在设计用户界面时,根据项目需求选择合适的方法,可以提供更健壮的输入控制。
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![dll](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
- 1
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
- thewking2013-05-10参考之后,可以实现功能,满足基本要求,非常感谢。
- huangfeishu2012-08-24恩,代码写的不错,解决问题了!
- Approx2012-10-10功能实现了,不过还有些不完善
![avatar](https://profile-avatar.csdnimg.cn/5da070e598094a31b0c41b989d685196_mirage1982.jpg!1)
- 粉丝: 59
- 资源: 1049
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)