Winform数字输入文本框
1、目的
使文本框只输入数字(包括小数)
2、步骤
在控件库中添加自定义控件(窗体);
修改代码;
3、代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lib
{
public partial class GTextBox : TextBox
{
public GTextBox()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
#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
{
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("最大小数位数不应小于最小小数位数");
}
}
}
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.TrimStart('0'); //移除前导的0
if (Text.IndexOf(".") == -1) //纯整数
{
integerLength = Text.Length;
decimalLength = 0;
}
else //整数和小数
{
integerLength = Text.IndexOf(".");
decimalLength = Text.Length - integerLength - 1;
//验证小数位是否符合最小值(不足补0)
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
}
}