[C#]重写文本控件的OnKeyPress和OnTextChanged进行录入数据校验
2006-01-05 18:32
357 查看
//记录OnKeyPress前的字符
string BeforKeyPressText = "";
//记录OnKeyPress执行时输入被视为无效(e.Handled = true;)的次数 注:一个汉字和一个英文字母的输入各执行一次OnKeyPress
int ErrKeyPressNum = 0;
/// <summary>
/// 实现输入控制:字符过滤
/// by arming 2005-11-28
/// </summary>
/// <param name="e"></param>
protected override void OnKeyPress(KeyPressEventArgs e)
{
//取出击键前的文本框文本
BeforKeyPressText = this.Text;
string[] regexExpressions=new string[2];
regexExpressions= (string[])CommonDataDefine.HtDTRE()[this.InputType.ToString()];
Regex regex = new Regex(regexExpressions[0]);
Match m = regex.Match(e.KeyChar.ToString());
if(!m.Success)
{
e.Handled = true;
//被处理为无效输入
++ErrKeyPressNum;
}
base.OnKeyPress (e);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(EventArgs e)
{
if( ErrKeyPressNum > 1 )
{
--ErrKeyPressNum;
return;
}
if( ErrKeyPressNum == 1 )
{
--ErrKeyPressNum;
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
if(this.InputType.ToString()=="Decimal4"&&this.Text.Trim()!=""&&this.Text.Trim()!="-")
{
try
{
decimal inputDecimal = System.Convert.ToDecimal(this.Text.Trim());
//数字长度
int p = this.MaxLength;
//小数点位置
int d1 = BeforKeyPressText.IndexOf('.');
int d2 = this.Text.Trim().IndexOf('.');
//
if(d2==-1&&this.Text.Trim().Length>p-4)
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
else if(d2!=-1&&(this.Text.Trim().Length-d2-1>4||d2>p-4))
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
}
catch
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
}
//整数 判断输入是否大于或小于Int所能保存的数值范围
if((this.InputType.ToString()=="Number"||this.InputType.ToString()=="Int")&&this.Text.Trim()!=""&&this.Text.Trim()!="-")
{
try
{
int inputNum = System.Convert.ToInt32(this.Text.Trim());
}
catch
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
}
//有两位小数点的小数 如Decimal(12,2)
if(this.InputType.ToString()=="Decimal"&&this.Text.Trim()!=""&&this.Text.Trim()!="-")
{
try
{
decimal inputDecimal = System.Convert.ToDecimal(this.Text.Trim());
//数字长度
int p = this.MaxLength;
//小数点位置
int d1 = BeforKeyPressText.IndexOf('.');
int d2 = this.Text.Trim().IndexOf('.');
if(d2==-1&&this.Text.Trim().Length>p-2)
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
else if(d2!=-1&&(this.Text.Trim().Length-d2-1>2||d2>p-2))
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
}
catch
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
}
base.OnTextChanged (e);
}
string BeforKeyPressText = "";
//记录OnKeyPress执行时输入被视为无效(e.Handled = true;)的次数 注:一个汉字和一个英文字母的输入各执行一次OnKeyPress
int ErrKeyPressNum = 0;
/// <summary>
/// 实现输入控制:字符过滤
/// by arming 2005-11-28
/// </summary>
/// <param name="e"></param>
protected override void OnKeyPress(KeyPressEventArgs e)
{
//取出击键前的文本框文本
BeforKeyPressText = this.Text;
string[] regexExpressions=new string[2];
regexExpressions= (string[])CommonDataDefine.HtDTRE()[this.InputType.ToString()];
Regex regex = new Regex(regexExpressions[0]);
Match m = regex.Match(e.KeyChar.ToString());
if(!m.Success)
{
e.Handled = true;
//被处理为无效输入
++ErrKeyPressNum;
}
base.OnKeyPress (e);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(EventArgs e)
{
if( ErrKeyPressNum > 1 )
{
--ErrKeyPressNum;
return;
}
if( ErrKeyPressNum == 1 )
{
--ErrKeyPressNum;
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
if(this.InputType.ToString()=="Decimal4"&&this.Text.Trim()!=""&&this.Text.Trim()!="-")
{
try
{
decimal inputDecimal = System.Convert.ToDecimal(this.Text.Trim());
//数字长度
int p = this.MaxLength;
//小数点位置
int d1 = BeforKeyPressText.IndexOf('.');
int d2 = this.Text.Trim().IndexOf('.');
//
if(d2==-1&&this.Text.Trim().Length>p-4)
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
else if(d2!=-1&&(this.Text.Trim().Length-d2-1>4||d2>p-4))
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
}
catch
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
}
//整数 判断输入是否大于或小于Int所能保存的数值范围
if((this.InputType.ToString()=="Number"||this.InputType.ToString()=="Int")&&this.Text.Trim()!=""&&this.Text.Trim()!="-")
{
try
{
int inputNum = System.Convert.ToInt32(this.Text.Trim());
}
catch
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
}
//有两位小数点的小数 如Decimal(12,2)
if(this.InputType.ToString()=="Decimal"&&this.Text.Trim()!=""&&this.Text.Trim()!="-")
{
try
{
decimal inputDecimal = System.Convert.ToDecimal(this.Text.Trim());
//数字长度
int p = this.MaxLength;
//小数点位置
int d1 = BeforKeyPressText.IndexOf('.');
int d2 = this.Text.Trim().IndexOf('.');
if(d2==-1&&this.Text.Trim().Length>p-2)
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
else if(d2!=-1&&(this.Text.Trim().Length-d2-1>2||d2>p-2))
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
}
catch
{
this.Text = BeforKeyPressText;
this.Select(BeforKeyPressText.Length,0);
}
}
base.OnTextChanged (e);
}
相关文章推荐
- VS2003 C#:重写键盘响应事件、动态创建控件、创建线程、在线程中使用委托在界面显示数据
- C# listview控件右击导出数据到txt文本
- C#通过API获取接收数据窗体控件句柄,并向该句柄发送文本信息
- C#实现对用户输入数据进行校验的类实例
- C# 读取文本内容,在Dev控件中显示数据
- C# 读取文本内容,在Dev控件中显示数据--<续>
- c# 读取文本中的一行用逗号连接数据,并对其进行排序
- C# treeview 控件动态绑定数据
- c#文本控件实现换行
- ASP.NET中DropDownList控件的数据绑定的C#语言描述
- 重写C#TreeView控件,提升TreeView控件档次
- C# WinFrom 用Execl做模版进行数据导入
- C#:ListView控件如何实现点击列表头进行排序
- WinForm(C#)CheckedlistBox绑定数据,并获得选中的值(ValueMember)和显示文本(DisplayMember
- 桶排序 C# 0(1)的辅助空间,o(n)的时间复杂度进行排序,数据量有上万条。
- C#中不同格式数据校验的正则表达式
- C# 读取Excel数据 并对日期是数字的进行处理
- 在C#中进行数据纵向不定行转横向列,多条信息成一行,例如员工薪资信息
- C#中对DataGridView列表中的数据进行查询并定位
- SpringMVC 使用 JSR-303 进行数据校验实例