一个只能输入数值型数据的文本框类实现(C#)
2007-03-13 23:00
627 查看
在软件开发时,经常面临数值类型数据的输入,如和限定文本框只能输入数值型数据,是一件麻烦事情。开发一个只能输入数值型数据的文本框可以简化很多开发工作,其代码如下,供参考。
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace PRisk
{
class TextBoxForDigit:TextBox
{
public ToolTip toolTip=new ToolTip() ;
public double max;
public double min;
public string tip = "输入数字型数据";
public TextBoxForDigit()
{
this.Validating +=
new System.ComponentModel.CancelEventHandler(NumberValidating);
this.MouseHover += new System.EventHandler(mMouseHover);
toolTip.SetToolTip(this , tip);
}
private void NumberValidating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!CheckIfTextBoxNumeric((TextBox)sender))
{
// myLabel.Text = "Has to be numeric";
e.Cancel = true;
}
}
private void mMouseHover(object sender, EventArgs e)
{
toolTip.SetToolTip(this, "输入数字型数据");
}
private bool CheckIfTextBoxNumeric(TextBox textBox)
{
bool isValid = true;
int pointCount = 0, negativeCount = 0;
if (textBox.Text == "")//判断是否为空
{
isValid = false; return isValid;
}
char ch;
for (int i = 0; i < textBox.Text.Length; i++)
{
ch = textBox.Text[i];
if (!System.Char.IsDigit(ch))
{
if (ch == '.') //如果是小数点
{
pointCount++;
if (i == 0 || i == textBox.Text.Length - 1)//如果在开始或结尾
{
textBox.Text = ""; isValid = false; return isValid;
}
}
if (ch == '-') //如果是负号
{
negativeCount++;
if (i!=0)//如果不在开始
{
textBox.Text = ""; isValid = false; return isValid;
}
}
if (ch != '-' && ch != '.')
{ textBox.Text = ""; isValid = false; return isValid; }
}
}
if (negativeCount > 1 || pointCount > 1)
{ textBox.Text = ""; isValid = false; return isValid; }
return isValid;
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace PRisk
{
class TextBoxForDigit:TextBox
{
public ToolTip toolTip=new ToolTip() ;
public double max;
public double min;
public string tip = "输入数字型数据";
public TextBoxForDigit()
{
this.Validating +=
new System.ComponentModel.CancelEventHandler(NumberValidating);
this.MouseHover += new System.EventHandler(mMouseHover);
toolTip.SetToolTip(this , tip);
}
private void NumberValidating(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!CheckIfTextBoxNumeric((TextBox)sender))
{
// myLabel.Text = "Has to be numeric";
e.Cancel = true;
}
}
private void mMouseHover(object sender, EventArgs e)
{
toolTip.SetToolTip(this, "输入数字型数据");
}
private bool CheckIfTextBoxNumeric(TextBox textBox)
{
bool isValid = true;
int pointCount = 0, negativeCount = 0;
if (textBox.Text == "")//判断是否为空
{
isValid = false; return isValid;
}
char ch;
for (int i = 0; i < textBox.Text.Length; i++)
{
ch = textBox.Text[i];
if (!System.Char.IsDigit(ch))
{
if (ch == '.') //如果是小数点
{
pointCount++;
if (i == 0 || i == textBox.Text.Length - 1)//如果在开始或结尾
{
textBox.Text = ""; isValid = false; return isValid;
}
}
if (ch == '-') //如果是负号
{
negativeCount++;
if (i!=0)//如果不在开始
{
textBox.Text = ""; isValid = false; return isValid;
}
}
if (ch != '-' && ch != '.')
{ textBox.Text = ""; isValid = false; return isValid; }
}
}
if (negativeCount > 1 || pointCount > 1)
{ textBox.Text = ""; isValid = false; return isValid; }
return isValid;
}
}
}
相关文章推荐
- C#实现对用户输入数据进行校验的类实例
- C#实现输入一个字符串输出该字符串字符的所有排列方式
- C#中的ComboBox实现只能选择不能输入,且下拉框中有默认值。
- c# 控制TextBox只能输入小数(只能输入一个小数点,小数点后只能输入两位,首位是小数点离开时自动补0,末位是小数点离开时自动清除)
- 用JS验证输入框只能输入数值型数据
- 用C#实现一个类,要求该类只能被实例化一次
- C# 实现验证文本框中输入的是数值型??
- 实现:C#窗体中的文本框只能输入中文汉字,其他输入无效。问:正则表达式怎么用?
- C#实现让程序只能打开一个实例(总结3方法)(转)
- 在ASP.NET(C#)中实现在一个文本框输入值后按回车时将光标移到下一个文本框
- 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n
- 一个jquery小插件, 实现限制文本框中输入的数据类型及长度
- C#中实现文本框只能输入数字
- 定义一个List集合并实例化对应的实现类对象。循环输入任意个整数,输入0结束输入循环,对集合中的数据进行从大到小的排序后输出
- dblink实现一个数据库用户只能查另一个库用户的数据
- JavaScript实现,控制一个文本框只能输入正整数,如输入不符合条件则文本框全部字体标红
- C#利用DataGridView实现数据的快速输入
- C#利用DataGridView实现数据的快速输入
- 一个使用C#语言实现的数据比较案例
- C# winform datagridview中如何实现鼠标右键点击一行数据出现一个带有删除的菜单,并能执行删除操作?