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

推荐订阅源

The Hacker News
The Hacker News
C
Cisco Blogs
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
S
Security Affairs
PCI Perspectives
PCI Perspectives
The Last Watchdog
The Last Watchdog
AWS News Blog
AWS News Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News and Events Feed by Topic
W
WeLiveSecurity
T
Tenable Blog
L
LINUX DO - 最新话题
T
Tor Project blog
Help Net Security
Help Net Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
爱范儿
爱范儿
O
OpenAI News
Hacker News - Newest:
Hacker News - Newest: "LLM"
Y
Y Combinator Blog
I
Intezer
C
Check Point Blog
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
S
Securelist
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
量子位
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Scott Helme
Scott Helme
H
Help Net Security
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
I
InfoQ
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
N
News | PayPal Newsroom
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes

博客园 - 刚刚

【转】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-28 · via 博客园 - 刚刚

见代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class FormFYSD : Form
    {
        public FormFYSD()
        {
            InitializeComponent();
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            bool result = false;
            //判断当前textBox是否为空
            if (!string.IsNullOrEmpty(textBox3.Text.Trim()))
            {
                //Regex rex = new Regex(@"^[0-9]*$");
                Regex rexFull = new Regex(@"^[0-9]+(.[0-9]{0,1})?$");
                //判断输入是否是退格键
                if (e.KeyChar == '\b')
                {
                    result = false;
                }
                else
                {
                    //判断是否匹配正则表达式
                    if (rexFull.IsMatch(textBox3.Text.Trim()) || rexFull.IsMatch(textBox3.Text.Trim() + e.KeyChar.ToString()))
                    {
                        //判断字符串中小数点的位数
                        if (Regex.Matches(textBox3.Text.Trim() + e.KeyChar.ToString(), "\\.").Count == 2)
                        {
                            result = true;
                        }
                        else
                        {
                            //判断输入字符是否是数字或者小数点
                            if (!(char.IsNumber(e.KeyChar) || e.KeyChar == (char)('.')))
                            {
                                result = true;
                            }
                            else
                            {
                                result = false;
                            }
                        }
                    }
                    else
                    {
                        result = true;
                    }
                }
            }
            else
            {
                if (e.KeyChar < '0' || e.KeyChar > '9')//这是不允许输入0-9数字
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            e.Handled = result;
        }
    }
}

这里需要注意添加一个引用:using System.Text.RegularExpressions;否则Regex无法识别。

本文转载自:https://jingyan.baidu.com/article/d3b74d640ff6cf1f77e6098f.html