Click here to Skip to main content
15,881,938 members
Articles / Programming Languages / C#
Tip/Trick

Textbox for numbers with a comma thousand seperator comma for currency C#

Rate me:
Please Sign up or sign in to vote.
4.60/5 (8 votes)
28 May 2013CPOL2 min read 65.6K   2.5K   4   8
A textbox usercontrol that displays numbers separated by comma such as currency.

Introduction

This is a simple and very usable usercontrol for entering numbers and displaying them such as currency. E. g., 1,200,000. I decided to make this a usercontrol because we can reuse it in multiple project. This usercontrol has two properties: "textWithcomma" that maintains the text of the usercontrol with a comma(','), "textWithoutcomma" that maintains the text of the usercontrol without comma. The second property is useful for math operations such as add ,...I have a method that skips the comma from the text and then saves it in the second property.

Using the code

  1. Use File->New Project and then select C# and press OK:
  2. Image 1

  3. Use Project->Add User Control and press OK:

    Image 2

  4. Now we have a stage like this to add components from the toolbox, in this project we need a TextBox for our usercontrol.

    Image 3

  5. Drag and drop a textbox from the toolbox:

    Image 4

  6. Right click on the textbox and select View Code. We will define two properties and a method which is used to skip comma from the text:
  7. The properties:

    C#
    public string textWithcomma { get; set; }
    public string textWithoutcomma { get; set; }

    And the method (this method is used to skip comma):

    C#
    public string skipComma(string str)
    {
        string[] ss = null;
        string strnew = "";
        if (str == "")
        {
            strnew = "0";
        }
        else
        {
            ss = str.Split(',');
            for (int i = 0; i < ss.Length; i++)
            {
                strnew += ss[i];
            }
        }
        return strnew;
    }
  8. Now we will right click on the textbox and select Properties and in the Properties dialog we will select events and then select the TextChanged event from the list and double click on it. In this method (TextChanged) we will write this code:
  9. C#
    if (textBox1.Text == "")
    {
        textBox1.Text =null;
        textWithcomma = "0"; //this property maintain the content of textbox with comma
        textWithoutcomma = "0";  //this property maintain the content of textbox without comma
    }
    else
    {
        if (textBox1.Text != "")
        {
            double d = Convert.ToDouble(skipComma(textBox1.Text));
            textBox1.Text=d.ToString("#,#",
              System.Globalization.CultureInfo.InvariantCulture);
            textWithcomma = textBox1.Text;//property maintain content of textbox with comma
            textWithoutcomma = skipComma(textBox1.Text);
            //property maintain content of textbox without comma
        }
    }
    textBox1.Select(textBox1.Text.Length, 0);
  10. Now we will right click on the textbox and select Properties and and then select events. Then from the event list we will choose the KeyPress event and double click on it. Now write the below code within this method. This code forces the textbox to accept numbers only:
    C#
    if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
    {
        e.Handled = true;
    }
  11. In the end Build the project by right clicking on the project (in this case WindowsFormApplication1) in Solution Explorer and selecting Build. Now you have a control in the Toolbox and you can use it several times in your project.

Please vote for me if this tip is useful!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFew questions Pin
Michael Ecklin27-Apr-17 3:48
Michael Ecklin27-Apr-17 3:48 
GeneralMy vote of 5 Pin
Ehsan yazdani rad9-Dec-16 18:56
Ehsan yazdani rad9-Dec-16 18:56 
Questionwhy it doen't execute this multiple Pin
morvarid.bz14-Mar-16 19:35
morvarid.bz14-Mar-16 19:35 
GeneralMy vote of 5 Pin
Ali Bagheri Shakib28-May-15 0:51
Ali Bagheri Shakib28-May-15 0:51 
Questionclear this textbox Pin
Member 114516056-Apr-15 5:45
Member 114516056-Apr-15 5:45 
Questionhow to add comma after each word Pin
Member 1034435926-Feb-14 19:21
Member 1034435926-Feb-14 19:21 
GeneralMy vote of 5 Pin
Member 1015531225-Oct-13 1:43
Member 1015531225-Oct-13 1:43 
GeneralRe: My vote of 5 Pin
mohammad amiri11-Mar-14 4:11
mohammad amiri11-Mar-14 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.