How to make a text box accept only numbers and number formats with commas?

I need a text box that:

(1) accepts only numbers as characters.

(2) Automatically continues to format numeric values ​​with commas when the user enters it.

For instance,

1 becomes 1.00 10 becomes 10.00 100 becomes 100.00 1000 becomes 1,000.00 10000 becomes 10,000.00 100000 becomes 1,00,000.00 

How to do it?

+6
source share
4 answers

Formatting a number when the user types in general works very poorly. For this you should use MaskedTextBox. There is a lot of code on the Internet that shows how to filter KeyPress, so only numbers can be entered. Most of them are trivially defeated using the Paste command.

A sensible way is to contact a user who is capable of basic skills, for example, dialing a number and gently reminding her that she was wrong. To do this, the Validating event is executed. This is also the perfect time to format your number. Add a new class to the project and paste this code:

 using System; using System.ComponentModel; using System.Windows.Forms; public class NumberBox : TextBox { public NumberBox() { Fraction = 2; } public ErrorProvider ErrorProvider { get; set; } [DefaultValue(2)] public int Fraction { get; set; } public event EventHandler ValueChanged; public decimal Value { get { return this.value; } set { if (value != this.value) { this.value = value; this.Text = Value.ToString(string.Format("N{0}", Fraction)); var handler = ValueChanged; if (handler != null) ValueChanged(this, EventArgs.Empty); } } } protected override void OnValidating(CancelEventArgs e) { if (this.Text.Length > 0 && !e.Cancel) { decimal entry; if (decimal.TryParse(this.Text, out entry)) { if (ErrorProvider != null) ErrorProvider.SetError(this, ""); Value = entry; } else { if (ErrorProvider != null) ErrorProvider.SetError(this, "Please enter a valid number"); this.SelectAll(); e.Cancel = true; } } base.OnValidating(e); } protected override void OnEnter(EventArgs e) { this.SelectAll(); base.OnEnter(e); } private decimal value; } 

Compile Remove the new NumberBox control from the top of the toolbar into your form. Also discard the ErrorProvider in the form so that printing errors can be reported modestly, set the ErrorProvider property of the new control. Change the fraction property if necessary. You can subscribe to the ValueChanged event to find out when the value was changed.

+4
source

There is a very simple way: using the TextChanged and Leave events.

 private void textBox_TextChanged(object sender, EventArgs e) { decimal theNumber; if(!decimal.TryParse((sender as TextBox).Text, out theNumber)) { (sender as TextBox).Text = string.empty; } } 

Do not try to format the number in the TextChanged event, it becomes messy. Then, when the user leaves the text box, change the format.

 private void textBox_Leave(object sender, EventArgs e) { // Since you were validating the number while typing now there is no need for TryParse decimal theNumber = decimal.Parse((sender as TextBox).Text); (sender as TextBox).Text = string.Format("{0:N2}", theNumber); } 

Note. It's simple and simple if you need something more thoughtful (best practices and all that) ... refer to Hans.

0
source

I don’t know if this will help, but here it is:

 try { double number = Convert.toDouble(textBox.Text); string[] digits = Regex.Split(textBox.Text, @"\W+"); int x = 0; List<string> finalNumber = new List<string>(); while(x < numbers.Length) { if(digits[x] == ".") break; if(x%3 = 0 && x != 0) { finalNumber.Add(","); } finalNumber.Add(digits[x]); x++; } string finalNumberJoined = ""; foreach(var digit in finalNumber) { finalNumberJoined = finalNumberJoined + digit; } } catch { //not a number } 
0
source

All Articles