why not use some numeric box instead of Text Box.
By the way you can use string numeric formatting.
I am developing a numeric box. And providing u the partial code. Hope this
helps.
Take a look at BuildExpressions() and Validate() functions.
public class NumericBox : System.Windows.Forms.TextBox
{
private char _CurrencySymbol;
private bool _AllowNegative;
private bool _AllowGroupSeperator;
private bool _AllowAccountingFormat;
private int _NumericPrecision;
private int _NumericScale;
private string _RegularExpression;
private string _FormatString;
private string _NumericText;
private string _DisplayText;
public NumericBox()
{
_CurrencySymbol='\0';
_AllowNegative=false;
_AllowAccountingFormat=false;
_AllowGroupSeperator=false;
_NumericPrecision=1;
_NumericScale=0;
_NumericText="";
_DisplayText="";
base.Text="";
BuildExpressions();
}
[Description("Currency symbol to be displayed with the numeric
value."),DefaultValue('\0')]
public char CurrencySymbol
{
set
{
_CurrencySymbol=value;
BuildExpressions();
Validate();
}
get
{
return _CurrencySymbol;
}
}
[Description("Allow negative numeric value."),DefaultValue(false)]
public bool AllowNegative
{
set
{
_AllowNegative=value;
BuildExpressions();
Validate();
}
get
{
return _AllowNegative;
}
}
[Description("Allow group seperator between digits."),DefaultValue(false)]
public bool AllowGroupSeperator
{
set
{
_AllowGroupSeperator=value;
BuildExpressions();
Validate();
}
get
{
return _AllowGroupSeperator;
}
}
[Description("Allow accounting format to display negative numeric
value."),DefaultValue(false)]
public bool AllowAccountingFormat
{
set
{
_AllowAccountingFormat=value;
BuildExpressions();
Validate();
}
get
{
return _AllowAccountingFormat;
}
}
[Description("Precision of numeric value."),DefaultValue(1)]
public int NumericPrecision
{
set
{
if(value>=0)
{
_NumericPrecision=value;
BuildExpressions();
Validate();
}
}
get
{
return _NumericPrecision;
}
}
[Description("Scale of numeric value."),DefaultValue(0)]
public int NumericScale
{
set
{
if(value>=0)
{
_NumericScale=value;
BuildExpressions();
Validate();
}
}
get
{
return _NumericScale;
}
}
[ReadOnly(true),DefaultValue("")]
public new string Text
{
get
{
return _DisplayText;
}
}
[Description("Numeric string value."),DefaultValue("")]
public string NumericText
{
set
{
_NumericText=value.Trim();
Validate();
}
get
{
return _NumericText;
}
}
private void BuildExpressions()
{
_RegularExpression = (_AllowNegative)?@"(\+|-)?"
"\+?";
_RegularExpression += (_NumericPrecision==0)?@"0"
"[0-9]{1,"+
_NumericPrecision.ToString() +"}";
_RegularExpression += (_NumericScale==0)?@""
"(\.[0-9]{1,"+
_NumericScale.ToString() +"})?";
_FormatString=(_CurrencySymbol=='\0')?@"":_CurrencySymbol.ToString();
_FormatString+=(_AllowGroupSeperator)?@"#,##0"
"#0";
if(_NumericScale!=0)
{
_FormatString+=".";
for(int i=1;i<=_NumericScale;i++)
_FormatString+="0";
}
_FormatString+=(_AllowAccountingFormat)?@";("+ _FormatString +");"
"";
}
private void Validate()
{
Regex r = new Regex(_RegularExpression);
Match m = r.Match(_NumericText);
if(m!=null)
_NumericText=m.ToString();
else
_NumericText="";
if(_NumericText!="")
{
_DisplayText=Decimal.Parse(_NumericText).ToString(_FormatString);
_NumericText=Decimal.Parse(_NumericText).ToString();
}
else
_DisplayText="";
if(this.Focused)
{
base.Text=_NumericText;
}
else
{
base.Text=_DisplayText;
}
}
protected override void OnEnter(EventArgs e)
{
base.Text=_NumericText;
this.Select(base.Text.Length,0);
base.OnEnter(e);
}
protected override void OnLeave(EventArgs e)
{
base.Text=_DisplayText;
base.OnLeave(e);
}
}