关于强制让一个TextBox控件只能输入数字(含小数)的方法
自已尝试过很多次﹐每次都不是很理想﹐现在尝试将其做成一个用户控件﹐与大家分享﹐同时希望大家能提供出更好的方法﹒
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace WinERP
{
public partial class UC_DecimalTextBox : UserControl
{
private int intLength = 3; // 整数部份的长度
private int decimalLength = 2; // 保留小数的位数
private ErrorProvider errorProvider; // 用于显示错误信息的ErrorProvider控件
[Description("设置整数部份的长度(1~100之间)")]
public int IntLength
{
get { return intLength; }
set
{
try
{
if (value < 1)
borderbox{
intLength = 1;
throw new Exception("整数部份的长序必须是1~100之间");
}
else if (value > 100)
{
intLength = 100;
throw new Exception("整数部份的长序必须是1~100之间");
}
else
{
intLength = value;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (decimalLength == 0)
{
textBox1.MaxLength = IntLength;
}
else
{
textBox1.MaxLength = intLength + decimalLength + 1;
}
}
}
}
[Description("设置小数部份的长度(0~10之间)")]
public int DecimalLength
{
get { return decimalLength; }
set
{
try
{
if (value < 0)
{
decimalLength = 0;
throw new Exception("小数部份的长序必须0~10之间");
}
else if (value > 10)
{
decimalLength = 10;
throw new Exception("小数部份的长序必须0~10之间");
}
else
{
decimalLength = value;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (decimalLength == 0)
{
textBox1.MaxLength = IntLength;
}
else
{
textBox1.MaxLength = intLength + decimalLength + 1;
}
}
}
}
[Description("用于显示错误信息的ErrorProvider控件")]
public ErrorProvider ErrorProvider
{
get { return errorProvider; }
set { errorProvider = value; }
}
[Description("文本框的边框样式")]
public BorderStyle TextBoxBorderStyle
{
get { return textBox1.BorderStyle; }
set { textBox1.BorderStyle = value; }
}
[Description("表示如何对齐TextBox控件中的文字")]
public HorizontalAlignment TextAlign
{
get { return textBox1.TextAlign; }
set { textBox1.TextAlign = value; }
}
[Description("控制是否可以编辑TextBox中的文字")]
public bool ReadOnly
{
get { return textBox1.ReadOnly; }
set { textBox1.ReadOnly = value; }
}
[Description("文本框中的文字")]
public override string Text
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
public UC_DecimalTextBox()
{
InitializeComponent();
}
/
/ 当控件调整大小时发生
private void UC_DecimalTextBox_Resize(object sender, EventArgs e)
{
textBox1.Width = this.Width - 6;
this.Height = textBox1.Height + 6; // 固定自定义控件的高﹐不允许调大
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == '\b') || (e.KeyChar == '.'))
{
e.Handled = false; // 允许输入
int pos = textBox1.SelectionStart;
int intLength = textBox1.Text.Length;
if ((intLength == textBox1.MaxLength) && (pos < textBox1.MaxLength))
{
textBox1.SelectionLength = 1;
if ((textBox1.SelectedText == ".") && (e.KeyChar != '.'))
{
++textBox1.SelectionStart;
}
}
if ((e.KeyChar == '.') && (textBox1.Text.IndexOf('.') > -1))
{
e.Handled = true;
if ((pos < IntLength - 1) && (textBox1.Text.Substring(pos, 1) == "."))
{
textBox1.SelectionStart = ++pos;
}
}
if ((decimalLength == 0) && (e.KeyChar == '.')) // 如果是保留零位小数﹐则不允许输入点号
{
e.Handled = true;
}
}
else
{
e.Handled = true; // 不允许输入
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
int pos = textBox1.SelectionStart;
string strValue = textBox1.Text.Trim();

if (decimalLength == 0)
{ // 当文本框中只输入整数时
while (strValue.Substring(0, 1) == "0")
{ // 去除整数部份最前右侧的
if (strValue.Length > 1)
{
strValue = strValue.Substring(1, strValue.Length - 1);
if (pos > 0)
{
--pos;
}
}
else
{
break;
}
}
textBox1.Text = strValue;
}
else
{ // 当文本框用于输入小数时
int pointPs = strValue.IndexOf('.');
if (pointPs < 0)
{
strValue += ".".PadRight(DecimalLength, '0');
}
if (pointPs == 0)
{
strValue = "0" + strValue;
textBox1.SelectionStart = ++pos;
}

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