Thousands Seperator with (,) in C# Dotnet

Thousands Seperator with (,) in C# Dotnet

This is a simple and very usable User Control for entering numbers and displaying them such as currency. E. g., 2,100,552. I decided to make this a User Control because we can reuse it in multiple project. This User Control has two properties: “textWithcomma” that maintains the text of the User Control with a comma(‘,’), “textWithoutcomma” that maintains the text of the User Control 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.

  1. Use File->New Project and then select C# and press OK:
  2. Use Project->Add User Control and press OK:
  3. Now we have a stage like this to add components from the toolbox, in this project we need a TextBox for our usercontrol.
  4. Drag and drop a textbox from the toolbox
  5. 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:The properties:
    public string textWithcomma { get; set; }
    public string textWithoutcomma { get; set; }
    

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

    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;
    }
    

     

  6. 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:
    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);
    

     

  7. 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:
    if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
    {
        e.Handled = true;
    }
    

     

  8. 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.

 

I hope it will help someone, somewhere. Feel free to Contact me anytime for any assistance.

One Comment

Post A Comment