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 ...
随机推荐
- centos6.5下使用yum完美搭建LNMP环境(php5.6)
准备工作 配置防火墙,开启80端口.3306端口删除原有的 iptables , 添加合适的配置 rm -rf /etc/sysconfig/iptables vi /etc/sysconfig/ip ...
- unidac 访问sql server 字符查询参数失效问题及解决办法
在帮朋友调试kbmmw 服务器的时候,发现用uindac 访问sql server作为后台时,碰见一个问题. 具体如下: cx.Close; cx.sql.add('select * from T w ...
- kaggle-泰坦尼克号Titanic-1
大家都熟悉的『Jack and Rose』的故事,豪华游艇倒了,大家都惊恐逃生,可是救生艇的数量有限,无法人人都有,副船长发话了『lady and kid first!』,所以是否获救其实并非随机,而 ...
- UVaLive 4128 Steam Roller (多决策最短路)
题意:给定一个图,r 根横线, c 根竖线.告诉你起点和终点,然后从起点走,每条边有权值,如果是0,就表示无法通行.走的规则是:如果你在下个路要转弯,会使这段路的时间加倍,但是如果一条路同时是这样,那 ...
- HDU1875 畅通工程再续 2017-04-12 19:52 48人阅读 评论(0) 收藏
畅通工程再续 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- Codeforces 768A Oath of the Night's Watch 2017-02-21 22:13 39人阅读 评论(0) 收藏
A. Oath of the Night's Watch time limit per test 2 seconds memory limit per test 256 megabytes input ...
- iOS9 Https技术预研
一.服务器需要做的事情: 1.要注意 App Transport Security 要求 TLS 1.2, 2.而且它要求站点使用支持forward secrecy协议的密码. 3.证书也要求是符合A ...
- Git Note - git tag
git tag is used to create labels, usually for version numbers. Format: git tag <TagName> <r ...
- python将json转csv
现有一个需求要将json转成excel,使用python将其转为csv格式,使用excel打开即可. import json import csv import codecs f = open('te ...
- easyui - using
using 是 easyloader.load 简写 using('calendar', function() { alert("加载calendar成功 ...