2024年1月8日发(作者:)
关于强制让一个TextBox控件只能输入数字(含小数)的方法
自已尝试过很多次﹐每次都不是很理想﹐现在尝试将其做成一个用户控件﹐与大家分享﹐同时希望大家能提供出更好的方法﹒
using System;
using c;
using entModel;
using g;
using ;
using ;
using ;
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)
{
intLength = 1;
throw new Exception("整数部份的长序必须是1~100之间");
}
else if (value > 100)
{
intLength = 100;
throw new Exception("整数部份的长序必须是1~100之间");
}
else
{
intLength = value;
}
}
catch (Exception ex)
{
(e);
}
finally
{
if (decimalLength == 0)
{
gth = IntLength;
}
else
{
gth = 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)
{
(e);
}
finally
{
if (decimalLength == 0)
{
gth = IntLength;
}
else
{
gth = intLength + decimalLength + 1;
}
}
}
}
[Description("用于显示错误信息的ErrorProvider控件")]
public ErrorProvider ErrorProvider
{
get { return errorProvider; }
set { errorProvider = value; }
}
[Description("文本框的边框样式")]
public BorderStyle TextBoxBorderStyle
{
get { return Style; }
set { Style = value; }
}
[Description("表示如何对齐TextBox控件中的文字")]
public HorizontalAlignment TextAlign
{
get { return ign; }
set { ign = value; }
}
[Description("控制是否可以编辑TextBox中的文字")]
public bool ReadOnly
{
get { return ly; }
set { ly = value; }
}
[Description("文本框中的文字")]
public override string Text
{
get { return ; }
set { = value; }
}
public UC_DecimalTextBox()
{
InitializeComponent();
}
// 当控件调整大小时发生
private void UC_DecimalTextBox_Resize(object sender, EventArgs e)
{
= - 6;
= + 6; // 固定自定义控件的高﹐不允许调大
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((r >= '0' && r <= '9') || (r == 'b') || (r == '.'))
{
d = false; // 允许输入
int pos = ionStart;
int intLength = ;
if ((intLength == gth) && (pos < gth))
{
ionLength = 1;
if ((edText == ".") && (r != '.'))
{
++ionStart;
}
}
if ((r == '.') && (f('.') > -1))
{
d = true;
if ((pos < IntLength - 1) && (ing(pos, 1) == "."))
{
ionStart = ++pos;
}
}
if ((decimalLength == 0) && (r == '.')) // 如果是保留零位小数﹐则不允许输入点号
{
d = true;
}
}
else
{
d = true; // 不允许输入
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
int pos = ionStart;
string strValue = ();
if (decimalLength == 0)
{ // 当文本框中只输入整数时
while (ing(0, 1) == "0")
{ // 去除整数部份最前右侧的
if ( > 1)
{
strValue = ing(1, - 1);
if (pos > 0)
{
--pos;
}
}
else
{
break;
}
}
= strValue;
}
else
{ // 当文本框用于输入小数时
int pointPs = f('.');
if (pointPs < 0)
{
strValue += ".".PadRight(DecimalLength, '0');
}
if (pointPs == 0)
{
strValue = "0" + strValue;
ionStart = ++pos;
}
// 自动使其保留两位小数
string[] strs = (new char[] { '.' });
strs[0] = strs[0].Length > 0 ? strs[0] : "0";
// 去除整数部份最前右侧的
while (strs[0].Substring(0, 1) == "0")
{
if (strs[0].Length > 1)
{
strs[0] = strs[0].Substring(1, strs[0].Length - 1);
if (pos > 0)
{
--pos;
}
}
else
{
break;
}
}
// 取得小数字数的数字,如果不足小数字长度﹐则以填补
if (strs[1].Length < decimalLength)
{
strs[1] = strs[1].PadRight(decimalLength, '0');
}
= strs[0] + "." + strs[1];
}
ionStart = pos;
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
if (errorProvider == null)
{
return;
}
or(this, "");
double dValue;
if (!se(, out dValue))
{
string strMsg = ("请输入整数部份不超过{0}位﹐且小数字不超过{1}位的有效数字!", IntLength, decimalLength);
if (decimalLength == 0)
{
strMsg = ("请输入不超过{0}位的有效整数!", IntLength);
}
or(this, strMsg);
= true;
}
}
}
}
文章来源:北大青鸟
发布者:admin,转转请注明出处:http://www.yc00.com/news/1704694755a1363266.html
评论列表(0条)