none
Separate 3 digits , 3 digits RRS feed

  • Question

  • i want to Separate 3 digits , 3 digits while writing in text box in c#, for example, it's 1000 and I wanna write to be a 1,000 it should add "," after 3 digits. it should be when i leave textbox or while i writing number it should add "," between numbers

    please help


    a baby in c#

    Friday, October 11, 2019 3:24 PM

Answers

  • You can handle TextChanged event.

    For example, for a textBox1 TextBox :

    private void textBox1_TextChanged(object sender, EventArgs e)
    {          
        if (!string.IsNullOrEmpty(textBox1.Text))
        {
            System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
            nfi.NumberGroupSeparator = ",";
            nfi.NumberDecimalSeparator = ".";
            nfi.CurrencyGroupSeparator = ".";
            nfi.PercentGroupSeparator = ".";
    
            long nValue = 0;
            try
            {
                nValue = long.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands, nfi);
            }
            catch (System.Exception se)
            {
                System.Windows.Forms.MessageBox.Show("Error : " + se.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                textBox1.Text = string.Format(nfi, "{0:N0}", nValue);
                textBox1.Select(textBox1.Text.Length, 0);
            }
        }
    }

    You can also set the ES_NUMBER style to allow only numbers


    • Edited by Castorix31 Saturday, October 12, 2019 12:43 PM
    • Marked as answer by Muhmmadhosein Sunday, October 13, 2019 12:32 PM
    Saturday, October 12, 2019 12:42 PM

All replies

  • >i want to Separate 3 digits , 3 digits while writing in text box in c#, for example, it's 1000 and I wanna write to be a 1,000 it should add "," after 3 digits. it should be when i leave textbox or while i writing number it should add "," between numbers

    You probably want to obey whatever the user's locale settings are for number formatting - use ToString("N").

    See here for more information on format strings:

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

    If you do need a specific custom format, follow the custom format link on that page.

    Dave

    Friday, October 11, 2019 7:49 PM
  • it didnt work

    could you write it in widnows form c# not console of c#


    a baby in c#

    Saturday, October 12, 2019 11:28 AM
  • You can handle TextChanged event.

    For example, for a textBox1 TextBox :

    private void textBox1_TextChanged(object sender, EventArgs e)
    {          
        if (!string.IsNullOrEmpty(textBox1.Text))
        {
            System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
            nfi.NumberGroupSeparator = ",";
            nfi.NumberDecimalSeparator = ".";
            nfi.CurrencyGroupSeparator = ".";
            nfi.PercentGroupSeparator = ".";
    
            long nValue = 0;
            try
            {
                nValue = long.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands, nfi);
            }
            catch (System.Exception se)
            {
                System.Windows.Forms.MessageBox.Show("Error : " + se.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                textBox1.Text = string.Format(nfi, "{0:N0}", nValue);
                textBox1.Select(textBox1.Text.Length, 0);
            }
        }
    }

    You can also set the ES_NUMBER style to allow only numbers


    • Edited by Castorix31 Saturday, October 12, 2019 12:43 PM
    • Marked as answer by Muhmmadhosein Sunday, October 13, 2019 12:32 PM
    Saturday, October 12, 2019 12:42 PM
  • thanks a lot

    a baby in c#

    Sunday, October 13, 2019 12:33 PM
  • Hi Muhmmadhosein,

    I'm glad to hear that your question has been solved.

    If you have other issue about VS debugger in the future, please feel free to contact us, we will try our best to provide a solution.

    Have a nice day.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Monday, October 14, 2019 7:55 AM