下面通过一个继承于textBox的用户控件实现该功能。只要设置不同的属性值就可以控制只输入金额,只能输入两位小数等功能。
程序中主要用到了两个属性:
一. ValidatingType 控制输入限制的类型,分贝为全数金额,只能为正数金额,只能为负数金额,只能为大小写字母
二. DecimalFraction 输入金额时最多只能输入的小数位数,默认为2位
代码主要是在用户控件中添加并重写了KeyPress事件,在该事件中根据用户设置属性的不同,对输入进行控制
主要代码如下:完整代码请从我的群中下载
一.字段和属性
Code
字段和属性#region 字段和属性
private TextBoxX tempTextBox = new TextBoxX();
private string strTemp = string.Empty;
private int decimalFraction = 2;
[DefaultValue(2), Description("小数位数")]
[Browsable(true)]
public int DecimalFraction
{
get { return decimalFraction; }
set { decimalFraction = value; }
}
private TextBoxValidatingType validatingType = TextBoxValidatingType.OnlyDigit;
[DefaultValue(TextBoxValidatingType.OnlyDigit), Description("选择输入限制的样式")]
[Browsable(true)]
public TextBoxValidatingType ValidatingType
{
get { return validatingType; }
set { validatingType = value; }
}
#endregion
字段和属性#region 字段和属性
private TextBoxX tempTextBox = new TextBoxX();
private string strTemp = string.Empty;
private int decimalFraction = 2;
[DefaultValue(2), Description("小数位数")]
[Browsable(true)]
public int DecimalFraction
{
get { return decimalFraction; }
set { decimalFraction = value; }
}
private TextBoxValidatingType validatingType = TextBoxValidatingType.OnlyDigit;
[DefaultValue(TextBoxValidatingType.OnlyDigit), Description("选择输入限制的样式")]
[Browsable(true)]
public TextBoxValidatingType ValidatingType
{
get { return validatingType; }
set { validatingType = value; }
}
#endregion
二. KeyPress事件
Code
KeyPress事件#region KeyPress事件
/**//// <summary>
/// KeyPress事件,用于限制输入字符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBoxCJ_KeyPress(object sender, KeyPressEventArgs e)
{
switch (this.validatingType)
{
case TextBoxValidatingType.OnlyDigit: //只能输入数字,包括正数,负数
OnlyDigit(sender, e);
break;
case TextBoxValidatingType.OnlyDigitPositive: //只能输入正数
OnlyDigitPositive(sender, e);
break;
case TextBoxValidatingType.OnlyDigitNegitive: //只能输入负数
OnlyDigitNegitive(sender, e);
break;
case TextBoxValidatingType.OnlyAlpha: //只能输入大小写字母
OnlyAlpha(e);
break;
default: //无任何输入限制
break;
}
}
private void OnlyDigit(object sender, KeyPressEventArgs e)
{//只能输入数字,包括正数,负数
convertQjToBj(e); //将全角转为半角
TextSelectedText(sender);
//判断是否为数字,'.','\b'
if (char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == '\b' || e.KeyChar == '-')
{
e.Handled = false;
}
else
{
e.Handled = true;
return;
}
if (!DecimalFractionJudge(e))
{
return;
}
//判断是否输入多个'.'字符 或 判断是否输入多个'-'字符
if ((strTemp.Contains(".") && e.KeyChar == '.') || (strTemp.Contains("-") && e.KeyChar == '-'))
{
e.Handled = true;
return;
}
//如果包含'-'字符,则必须是第一个
if ((tempTextBox.SelectionStart != 0) && (e.KeyChar == '-'))
{
e.Handled = true;
return;
}
//第一个字符不能为'.'
if ((strTemp == "-" || tempTextBox.SelectionStart == 0) && (e.KeyChar == '.'))
{
e.Handled = true;
return;
}
}
#endregion
KeyPress事件#region KeyPress事件
/**//// <summary>
/// KeyPress事件,用于限制输入字符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBoxCJ_KeyPress(object sender, KeyPressEventArgs e)
{
switch (this.validatingType)
{
case TextBoxValidatingType.OnlyDigit: //只能输入数字,包括正数,负数
OnlyDigit(sender, e);
break;
case TextBoxValidatingType.OnlyDigitPositive: //只能输入正数
OnlyDigitPositive(sender, e);
break;
case TextBoxValidatingType.OnlyDigitNegitive: //只能输入负数
OnlyDigitNegitive(sender, e);
break;
case TextBoxValidatingType.OnlyAlpha: //只能输入大小写字母
OnlyAlpha(e);
break;
default: //无任何输入限制
break;
}
}
private void OnlyDigit(object sender, KeyPressEventArgs e)
{//只能输入数字,包括正数,负数
convertQjToBj(e); //将全角转为半角
TextSelectedText(sender);
//判断是否为数字,'.','\b'
if (char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == '\b' || e.KeyChar == '-')
{
e.Handled = false;
}
else
{
e.Handled = true;
return;
}
if (!DecimalFractionJudge(e))
{
return;
}
//判断是否输入多个'.'字符 或 判断是否输入多个'-'字符
if ((strTemp.Contains(".") && e.KeyChar == '.') || (strTemp.Contains("-") && e.KeyChar == '-'))
{
e.Handled = true;
return;
}
//如果包含'-'字符,则必须是第一个
if ((tempTextBox.SelectionStart != 0) && (e.KeyChar == '-'))
{
e.Handled = true;
return;
}
//第一个字符不能为'.'
if ((strTemp == "-" || tempTextBox.SelectionStart == 0) && (e.KeyChar == '.'))
{
e.Handled = true;
return;
}
}
#endregion