C#数字输⼊框(⾃定义控件)代码
public partial class nTextBox : System.Windows.Forms.TextBox
{
#region⾃定义成员
private int maxIntegerLength = 10;
private int minIntegerLength = 0;
private int maxDecimalLength = 4;
private int minDecimalLength = 0;
private int integerLength;
private int decimalLength;
#endregion
#region⾃定义属性
///<summary>
///最⼤整数位数
///</summary>
public int MaxIntegerLength
{
get
{
return maxIntegerLength;
}
set
{
if (value >= 0 && value >= minIntegerLength)
{
maxIntegerLength = value;
}
else
{
throw new Exception("最⼤整数位数不应⼩于最⼩整数位数");
}
}
}
/
//<summary>
///最⼩整数位数
///</summary>
public int MinIntegerLength
{
get
textbox控件边框设置{
return minIntegerLength;
}
set
{
if (value >= 0 && value <= maxIntegerLength)
{
minIntegerLength = value;
}
else
{
throw new Exception("最⼩整数位数不应⼤于最⼤整数位数");
}
}
}
///<summary>
/
//最⼤⼩数位数
///</summary>
public int MaxDecimalLength
{
get
{
return maxDecimalLength;
}
set
{
if (value >= 0 && value >= minDecimalLength)
{
maxDecimalLength = value;
}
else
{
throw new Exception("最⼤⼩数位数不应⼩于最⼩⼩数位数");
}
}
}
///<summary>
///最⼩⼩数位数
/
//</summary>
public int MinDecimalLength
{
get
{
return minDecimalLength;
}
set
{
if (value >= 0 && value <= maxDecimalLength)
{
minDecimalLength = value;
}
else
{
throw new Exception("最⼩⼩数位数不应⼤于最⼤⼩数位数");
}
}
}
#endregion
#region重写⽅法
protected override void OnKeyPress(KeyPressEventArgs e)
{
int editIndex = SelectionStart;        //获取当前编辑位
if (e.KeyChar == (char)Keys.Back) return;  //放⾏"退格"键
if (e.KeyChar.Equals('.') || Char.IsNumber(e.KeyChar)) //过滤⾮数字与⾮⼩数点            {
if (Text.IndexOf(".") > -1)    //是否存在⼩数点
{
//禁⽌重复输⼊⼩数点
if (e.KeyChar.Equals('.'))
{
e.Handled = true;
return;
}
else
{
if (SelectedText.Length > 0)
{
return;
}
integerLength = Text.IndexOf(".");
decimalLength = Text.Length - integerLength - 1;
//控制最⼤⼩数位数
if (decimalLength >= maxDecimalLength && editIndex > Text.IndexOf("."))                        {
e.Handled = true;
return;
}
//控制最⼤整数位数
if (integerLength >= maxIntegerLength && editIndex <= Text.IndexOf("."))                        {
e.Handled = true;
return;
}
}
}
else
{
//控制最⼤整数位数
integerLength = Text.Length;
if (integerLength == maxIntegerLength && !e.KeyChar.Equals('.'))
{
e.Handled = true;
}
}
}
else
{
e.Handled = true;
}
base.OnKeyPress(e);
}
protected override void OnLeave(EventArgs e)
{
if (Text == null || Text == "") return;
Text = Text.TrimStart('0');
//取整数位数与⼩数位数
if (Text.IndexOf(".") == -1)
{
integerLength = Text.Length;
decimalLength = 0;
}
else
{
integerLength = Text.IndexOf(".");
decimalLength = Text.Length - integerLength - 1;
//验证⼩数位数是否符合最⼩值(不⾜补零)
if (decimalLength < minDecimalLength)
{
Text = Text.PadRight(integerLength + minDecimalLength + 1, '0');
}
}
//整数未输⾃动补零
if (integerLength == 0)
{
Text = "0" + Text;
}
//验证整数位数是否符合最⼩值
if (integerLength < minIntegerLength)
{
Focus();
Select(0, integerLength);
}
//验证整数位数是否符合最⼤值
if (integerLength > maxIntegerLength)            {
Focus();
Select(0, integerLength);
}
base.OnLeave(e);
}
#endregion
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。