c#中设置像数量,价格,金额等的textbox的限制条件,用户只能输入数字或小数(小数及负号只能输一次)
来源:互联网 发布:什么软件可以发表文章 编辑:程序博客网 时间:2024/04/29 13:41
//判断按键是不是要输入的类型。
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar !=46 && (int)e.KeyChar !=45 )
e.Handled = true;
//小数点的处理。
if ((int)e.KeyChar == 46){ //小数点
if (txtAdjustment.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else{ //处理不规则的小数点
char strAdjustment = Convert.ToChar(txtAdjustment.Text.Substring(0,1));
if((int)strAdjustment == 45 && txtAdjustment.Text.Length == 1) //小数点不能在负号后面
e.Handled = true;
else{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(txtAdjustment.Text, out oldf);
b2 = float.TryParse(txtAdjustment.Text + e.KeyChar.ToString(), out f);
if (b2 == false){
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}else if((int)e.KeyChar == 45){ //负号
if (txtAdjustment.Text.Length >= 1)
e.Handled = true; //负号只能在第一位
}
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar !=46 && (int)e.KeyChar !=45 )
e.Handled = true;
//小数点的处理。
if ((int)e.KeyChar == 46){ //小数点
if (txtAdjustment.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else{ //处理不规则的小数点
char strAdjustment = Convert.ToChar(txtAdjustment.Text.Substring(0,1));
if((int)strAdjustment == 45 && txtAdjustment.Text.Length == 1) //小数点不能在负号后面
e.Handled = true;
else{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(txtAdjustment.Text, out oldf);
b2 = float.TryParse(txtAdjustment.Text + e.KeyChar.ToString(), out f);
if (b2 == false){
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}else if((int)e.KeyChar == 45){ //负号
if (txtAdjustment.Text.Length >= 1)
e.Handled = true; //负号只能在第一位
}
参考文档:http://blog.sina.com.cn/s/blog_a9091a33010162iv.html
0 0
- c#中设置像数量,价格,金额等的textbox的限制条件,用户只能输入数字或小数(小数及负号只能输一次)
- asp.net中前台限制TextBox文本框只能输入数字和小数(或数字和字母)
- c#中textbox只能输入数字, 包括小数
- C# winform中限制只能输入小数
- c# 如何让textbox只能输入数字和小数
- VB.net中限制TextBox只能输入数字(小数、负数)
- 限制用户输入 只能是数字,小数,负数。
- 限制只能输入小数
- 代码设置editext只能输入数字和整数以及只能输入两位小数的处理
- UITextField 限制只能输入数字和小数点,只能有两位小数
- TextBox限制只能输入字母或数字
- C# 设置文本框等控件只能输入 正负整数或小数
- 限制只能输入整数、小数的输入框
- C#只能输入数字的textbox
- C#的winform中控制TextBox中只能输入数字?
- Silverlight限制TextBox只能输入整数或者小数
- Silverlight限制TextBox只能输入整数或者小数
- 仿微信提现金额,输入金额只能有两位小数及文字监听的改变
- debug
- 2015 Xcode7 真机调试(开发者)以及P12配置
- ansbile简单应用
- KAGGLE ENSEMBLING GUIDE---模型融合
- 文件名批量修改工具V2.0 绿色版
- c#中设置像数量,价格,金额等的textbox的限制条件,用户只能输入数字或小数(小数及负号只能输一次)
- JUnit详解系列
- Android下载大图片(例如微博长图片)会出现OOM down掉问题
- vue中引入jquery写轮播图
- VDPAU 的简介
- 【IMWeb训练营作业1】-TodoList
- SpringBoot学习笔记(四) SpringBoot Web相关的自动配置
- FastRCNN 训练自己数据集(三)——训练和检测
- ID3 Classification using data.tree