限制用户输入 只能是数字,小数,负数。
2016-03-25 10:32
323 查看
实现用户输入只能是数字,小数,负数,且小数和负数只能输入一次。
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45 && e.KeyChar != 46)
{
e.Handled = true;
}
//输入为负号时,只能输入一次且只能输入一次
if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45 && e.KeyChar != 46)
{
e.Handled = true;
}
//输入为负号时,只能输入一次且只能输入一次
if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true;
if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;
}
相关文章推荐
- Oracle查看用户角色权限的语句
- Papers about DL
- time_t和struct tm之间的转换
- C#控制鼠标移动
- urllib2加入header及解决中文乱码问题
- jquery学习-01-基础篇
- jave获取视频时长
- ibatis 批量插入,批量更新,批量删除,xml与Java代码实现方式
- MySQL权限篇之CREATE ROUTINE与CREATE TABLESPACE
- 安装win32com报错-Python Version 2.7 required which was not found in the registry 问题解决 .
- Spring事务Transaction配置的五种注入方式详解
- 图像分割之霍夫变换(Hough)
- Mysql授权root用户使用远程客户端连接
- android4.x 模拟器 出现软键盘
- LinkedHashMap源码解析
- Detach a Database
- 第二排图片不从左边排的原因是图片的大小不一样
- javaweb学习总结(十五)——JSP基础语法
- 第7课 循环语句
- Iaas-cloudstack概念