Hi,
I went though so many web pages and i couldn't find a simple currency masked TextBox which simple and easy to understand. Below is a simple code which was implemented by me and it is very easy to understand and you will able to modify it easily according to your requirements.
private string val = "";
private void textBox16_TextChanged(object sender, EventArgs e)
{
int count = this.textBox16.Text.Split('.').Length - 1;
if (this.textBox16.Text.Length==0)
{
return;
}
if (!TextisValid(this.textBox16.Text) || count>1)
{
this.textBox16.Text =val;
this.textBox16.SelectionStart = this.textBox16.Text.Length;
}
if(this.textBox16.Text.ToLowerInvariant().IndexOf('.') != -1){
string[] words = this.textBox16.Text.Split('.');
if (words[1].Length > 2)
{
this.textBox16.Text = val;
this.textBox16.SelectionStart = this.textBox16.Text.Length;
}
}
val = this.textBox16.Text;
//this.textBox16.Text = "x.xx";
}
private bool TextisValid(string text)
{
//Regex money = new Regex(@"^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");
Regex money = new Regex(@"^-*[0-9,\.]+$");
return money.IsMatch(text);
}
0 comments:
Post a Comment