-1

I want to know how can I add a thousand separator like a comma for my textBox while I am typing a number in it.

i.e. 1,500 instead of 1500.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86

2 Answers2

1

Fortunately, I solved my problem.

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (textBox1.Text == "" || textBox1.Text == "0") return;                         
    decimal price;                                                                   
    price = decimal.Parse(textBox1.Text, System.Globalization.NumberStyles.Currency);
    textBox1.Text = price.ToString("#,#");                                           
    textBox1.SelectionStart = textBox1.Text.Length;                                  
}

we can write this code in the TextChanged method of our textBox to add a thousand separator for the textBox.

by the way, If you wanted to change it later to the first state or if you wanted to use the number in the textbox, in the database, you can use the Replace method.

i.e. textBox1.Text.Replace(",","");

Hope you find it useful.

  • 2
    *"if you wanted to use the number in the textbox, in the database, you can use the Replace method."* Rather use a decimal type in the DB – Cid Jan 08 '21 at 07:58
  • Yep, that's completely right bro but it definitely takes up more space. – Alireza Mahzad Jan 08 '21 at 08:04
  • 'takes up more space' - you mean disc space? Pretty inessential imo. Bytes are cheap these days... You want to store a `price` and i assume you need it for calculations *anywhere* in your application. You'd have to parse it back and forth every time you want to use it as number; that would bother me a lot more. – nilsK Jan 08 '21 at 08:52
  • 1
    in europe the `, ` is not used for thousand separator, but the `. ` is. So be very very carefull when you retrieve information from another location – GuidoG Jan 08 '21 at 10:49
  • Thanks for reminding me GuidoG. Actually, as I am from the middle east, I used ' , ' but as you said we have to use ' . ' for Europe. – Alireza Mahzad Jan 08 '21 at 14:58
  • @GuidoG not everywhere. In France, the thousand separator is space – Cid Jan 08 '21 at 18:05
  • 1
    @Cid yes, another good reason to NOT use a constant for the thousand separator – GuidoG Jan 10 '21 at 10:14
0

Method 1 - Add a thousand separator to a TextBox - If you want to add a thousand separator to a TextBox immediately after the client enters a number to the TextBox you can use the code below in the TextChanged event of the TextBox.

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (textBox1.Text == "" || textBox1.Text == "0") return;
    decimal number;
    number = decimal.Parse(textBox1.Text, System.Globalization.NumberStyles.Currency);
    textBox1.Text = number.ToString("#,#");
    textBox1.SelectionStart = textBox1.Text.Length;
}

Method 2 - Add a thousand separator to Label - If you want to add a thousand separators to a label that its text is stable and does not change as a TextBox, you can use the code below that uses NumberFormatInfo helper class.

var separator= new System.Globalization.NumberFormatInfo() {
    NumberDecimalDigits = 0,
    NumberGroupSeparator = "."
};

var import = 1234567890;
var export = import.ToString("N", separator);

Method 3 - The easiest way to do it - The easiest way to add a thousand separator to a text in a TextBox or a Label is to use the ToString("N") method like the code below,

Convert.ToDouble("1234567.12345").ToString("N")
dbc
  • 104,963
  • 20
  • 228
  • 340