C#-WinForm-TextBox中只能输入数字的几种常用方法(C#)
- 方法一:
- private void tBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == 0x20) e.KeyChar = (char)0; //禁止空格键
- if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; //处理负数
- if (e.KeyChar > 0x20)
- {
- try
- {
- double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
- }
- catch
- {
- e.KeyChar = (char)0; //处理非法字符
- }
- }
- }
- 方法二:
- private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
- 或者
- private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
- 方法三:
- private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
- {
- if(e.KeyChar!='\b')//这是允许输入退格键
- {
- if((e.KeyChar<'0')||(e.KeyChar>'9'))//这是允许输入0-9数字
- {
- e.Handled = true;
- }
- }
- }
- 方法四:
- private void textBox1_Validating(object sender, CancelEventArgs e)
- {
- const string pattern = @"^\d+\.?\d+{1}quot;;
- string content = ((TextBox)sender).Text;
- if (!(Regex.IsMatch(content, pattern)))
- {
- errorProvider1.SetError((Control)sender, "只能输入数字!");
- e.Cancel = true;
- }
- else
- errorProvider1.SetError((Control)sender, null);
- }
- 方法五:
- private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
- {
- if(e.KeyChar=='.' && this.textBox1.Text.IndexOf(".")!=-1)
- {
- e.Handled=true;
- }
- if(!((e.KeyChar>=48 && e.KeyChar<=57) || e.KeyChar=='.' || e.KeyChar==8))
- {
- e.Handled=true;
- }
- }
- 方法六:
- private void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
- {
- e.Handled = true;//消除不合适字符
- }
- else if (Char.IsPunctuation(e.KeyChar))
- {
- if (e.KeyChar != '.' || this.textBox1.Text.Length == 0)//小数点
- {
- e.Handled = true;
- }
- if (textBox1.Text.LastIndexOf('.') != -1)
- {
- e.Handled = true;
- }
- }
- }
- 方法七:
- 利用ASCII码处理办法、
- {
- if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46))
- e.Handled = true;
- ================48代表0,57代表9,8代表空格,46代表小数点
- }
C#-WinForm-TextBox中只能输入数字的几种常用方法(C#)的更多相关文章
- Winform TextBox中只能输入数字的几种常用方法(C#)
方法一: private void tBox_KeyPress(object sender, KeyPressEventArgs e) { ; //禁止空格键 )) return; //处理负数 if ...
- C#-WinForm-Winform TextBox中只能输入数字的几种常用方法(C#)
方法一: private void tBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 0x20) e.KeyCh ...
- C#的winform中控制TextBox中只能输入数字
C#的winform中控制TextBox中只能输入数字 private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPr ...
- winform中如何在TextBox中只能输入数字(可以带小数点)
可以采用像web表单验证的方式,利用textbox的TextChanged事件,每当textbox内容变化时,调用正则表达式的方法验证,用一个label在text后面提示输入错误,具体代码如下: pr ...
- 限定textbox中只能输入数字的小方法
在textbox中加入onkeyup="this.value=this.value.replace(/\D/g,' ')"即可实现这一功能 验证数字的正则表达式:^[0-9]*$或 ...
- 控制input标签中只能输入数字以及小数点后两位
js 代码如下: /* 控制input标签中只能输入数字 和小数点后两位 */ function checkNum(obj) { //检查是否是非数字值 if (isNaN(obj.value)) { ...
- .net(c#) winform文本框只能输入数字,不能其他非法字符
private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { //阻止从键盘输入键 ...
- input输入框中只能输入数字,非数字字符自动清除
前言:项目中有个缴纳保证金的功能,要是输入框只能输入数字,不能输入其他字符. ①HTML代码:<input class="input-box" type="text ...
- 关于input只能输入数字的两种小方法
第一种: 直接给input标签 name赋值如下 <input name="start_price" id="start_price" type=&quo ...
随机推荐
- FIX protocol tutorial : Fix Session is not connecting how to diagnose it ?
In this blog post of FIX protocol tutorial series I would like to share my experience with connectiv ...
- 运行Junit单测时遇到的问题
现在有两个办法解决: 1.junit版本降到4.10 2.导入hamcrest-core-1.3.jar 官网:JUnit now uses the latest version of Hamcres ...
- 特性(property)
6.4 特性(property) 1 什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 import math class Circle: def ...
- 全球数据库-->基金/管理产品-->基金分析/新闻/报告
加拿大共同基金 澳大利亚投资信托 美国ETF 美国共同基金 英国投资信托基金 名称 分析师名称 分析日期 晨星分析师评级 晨星简报
- TableLayout 里的TextView等组的LayoutParams参数问题
TableLayout 里的TextView等组的LayoutParams参数不能是LinearLayout.LayoutParams这样来定义, 只能是用TableRow.LayoutParams ...
- Uploadify多文件上传插件.NET使用案例+PHP使用案例
ploadify是一个非常好用的多文件上传插件 插件下载:http://www.uploadify.com 下载后需要用到的文件: 接下来就是直接添加代码: Default.aspx代码 <%@ ...
- 如何讓 iOS UIWebView 連線時傳送自訂 Cookie 的方法[转]
利用 NSHTTPCookieStorage 管理 Cookie 傳送 在 iOS 中如果自行建立 UIWebView 來開啟遠端站台資料,這時可以透過以下方法加入 Cookie.原理是透過 iOS ...
- swift 自定义弹框
// // ViewController.swift // animationAlert // // Created by su on 15/12/9. // Copyright © 2015 ...
- 树莓派3下安装TL-WN722N无线网卡驱动
最近在搞树莓派,我的是国产的树莓派3卡片电脑,想整成一个无线加有线的路由器(树莓派3.TL-WN722N无线网卡.集线器)或者是搭个web认证的WIFI钓鱼热点玩玩. 一开始就遇到问题了,连无线网卡的 ...
- IOC容器基本原理
1 IoC容器的概念 IoC容器就是具有依赖注入功能的容器,IoC容器负责实例化.定位.配置应用程序中的对象及建立这些对象间的依赖.应用程序无需直接在代码中new相关的对象,应用程序由IoC容器进行 ...