惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
小众软件
小众软件
D
Docker
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
V
V2EX
博客园 - 叶小钗
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
IT之家
IT之家
博客园 - 司徒正美
M
MIT News - Artificial intelligence
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog

博客园 - 刚刚

【转】Winform中textBox通过正则表达式限制只能输入数字且是两位小数 【转】C# winform窗体间传值(使用委托或事件) 【转】Visual Studio2019报错/plugin.vs.js,行:1074,错误:缺少标识符、字符串或数字的解决方法 奇虎360诉腾讯QQ垄断案之我见(3Q大战之我见) 超图软件(SuperMap)学习资料:桌面软件(Deskpro) ☠360与腾讯QQ的战争☠之我见 “密码人”越来越多将会出现“密码危机” SuperMap Objects开发——下载安装地址、许可配置和控件加载 决定将本博客技术知识从VS.Net转型SuperMap产品动态与开发 八零年代人的2009年 微软自打击盗版风波后推出了多种正版推广限时促销活动 【转】JavaScript写的Cookie类 ASP.NET开发在JavaScript有中文汉字时出现乱码时简单有效的解决方法 怀疑自己的笔记本电脑可能受到了网络攻击被人监视或操控 Visual Studio.Net 2005中用SqlDataSource处理数据库特殊数据类型 Visual Studio.Net 2005中验证控件的一个使用方法 对微软2008年10月20日后推行的打击盗版举措的个人看法 【转】娱乐八卦——关于孙悟空的授业师傅的分析 【转】GIS原理学习
【转】WinForm中TextBox只能输入数字
刚刚 · 2020-11-27 · via 博客园 - 刚刚

只能输入整数

方法一

private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
    //如果输入的不是退格和数字,则屏蔽输入
    if (!(e.KeyChar == '\b' || (e.KeyChar >= '0' && e.KeyChar <= '9')))
    {
        e.Handled = true;
    }
}

e.KeyChar >= ‘0’ && e.KeyChar <= ‘9’ //表示输入的是数字
e.Handled = true; //true表示已经处理该事件,则屏蔽输入

方法二

private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
    //如果输入的不是退格和数字,则屏蔽输入
    if (!(e.KeyChar == 8 || (e.KeyChar >= 48 && e.KeyChar <= 57)))
    {
        e.Handled = true;
    }
}

8代表退格,48代表0,57代表9,46代表小数点

方法三

private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
    //如果输入的不是退格和十进制数字,则屏蔽输入
    if (!(e.KeyChar == '\b' || char.IsDigit(e.KeyChar)))
    {
        e.Handled = true;
    }
}

方法四

private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
    //如果输入的不是退格且不能转为整数,则屏蔽输入
    if (!(e.KeyChar == '\b' || int.TryParse(((TextBox)sender).Text + e.KeyChar.ToString(), out int i)))
    {
        e.Handled = true;
    }
}

只能输入小数

方法一

private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
    //当前输入的是"."且(输入框已经有“.”或者文本框没有内容),则屏蔽输入
    if (e.KeyChar == '.' && (((TextBox)sender).Text.IndexOf(".") != -1 || ((TextBox)sender).Text.Length == 0))
    {
        e.Handled = true;
    }
    //如果输入的不是退格、数字和点,则屏蔽输入
    if (!(e.KeyChar == '\b' || (e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.'))
    {
        e.Handled = true;
    }
}

方法二

private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
    //如果输入的不是退格且不能转为小数,则屏蔽输入
    if (!(e.KeyChar == '\b' || float.TryParse(((TextBox)sender).Text + e.KeyChar.ToString(), out float f)))
    {
        e.Handled = true;
    }
}

只能输入数字(包含负数)

private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
    //如果输入的不是负号,退格且不能转为小数,则屏蔽输入
    if (!(e.KeyChar == '-'|| e.KeyChar == '\b' || float.TryParse(((TextBox)sender).Text + e.KeyChar.ToString(), out float f)))
    {
        e.Handled = true;
    }
}

本文转载自:https://blog.csdn.net/weixin_38211198/article/details/89214705