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

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 孙英雄

C# 窗体抖动 “嵌入式资源”的调用 - 孙英雄 - 博客园 SQL2000与Oracle的分布式操作 只能输入数字的TextBox DevExpress7.3.4部分控件继承后无法编辑解决方案 Mssql安全设置 .NET中如何生成静态页 - 孙英雄 - 博客园 ASP.NET跨页面传值技巧总结 预报:卡巴斯基v6.0个人版KEY获取器---修正版 - 孙英雄 - 博客园 动态读取App.Config WEB开发调试利器:Firebug SQL函数 关注SharpDevelop的一些问题 又一款Auto病毒专杀工具,可解决双击无法打开盘符的问题 Office2007体验及下载 简单讲述基于SQL SERVER 分页的存储过程 Access中的模糊查询 - 孙英雄 - 博客园 DataGrid控件排序方法 Asp.net中的常用小技巧
只能输入数字的TextBox---补充
孙英雄 · 2008-02-15 · via 博客园 - 孙英雄

原代码:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
if(!char.IsDigit(e.KeyChar)) e.Handled = true;
            
//'\b'是退格键值
            if(e.KeyChar=='\b'||e.KeyChar=='.') e.Handled = false;
        }

以上代码是无法限制全角数字输入的.而在项目中全角数字是不能算真正的数字,因为在参加计算时全角可能会出错.
修改的代码:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
//全角占一个汉字,半角点半个汉字,所以在字节上是不同的
            
//全角数字"KeyChar"=2,半解数字"KeyChar"=1
            byte[] array = System.Text.Encoding.Default.GetBytes(e.KeyChar.ToString());
            
//array.LongLength,而不是array.Length
            if (!char.IsDigit(e.KeyChar)|| array.LongLength==2) e.Handled = true;
            
//'\b'是退格键值
            if (e.KeyChar == '\b' || e.KeyChar == '.') e.Handled = false;
             
        }