Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Thousand Seperator in TextBox

Thousand Seperator in TextBox

Scheduled Pinned Locked Moved C#
help
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    freshonlineMax
    wrote on last edited by
    #1

    Hello Please help me to show thousand seperator in textbox like: 25000000>25,000,000 I found a formul like below at textchanged event: string myStr = textBox1.Text; TextBox1.Text = myStr.FormatString("#,###"); But it causes error at : Int a = Convert.ToInt16(TextBox1.Text); I want to seperate numbers in textbox anyway. I couldn't use "MaskedTextBox" Please help me. Regards

    L 0 L 3 Replies Last reply
    0
    • F freshonlineMax

      Hello Please help me to show thousand seperator in textbox like: 25000000>25,000,000 I found a formul like below at textchanged event: string myStr = textBox1.Text; TextBox1.Text = myStr.FormatString("#,###"); But it causes error at : Int a = Convert.ToInt16(TextBox1.Text); I want to seperate numbers in textbox anyway. I couldn't use "MaskedTextBox" Please help me. Regards

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You could remove the "," before parsing (btw why aren't you using short.Parse?)

      1 Reply Last reply
      0
      • F freshonlineMax

        Hello Please help me to show thousand seperator in textbox like: 25000000>25,000,000 I found a formul like below at textchanged event: string myStr = textBox1.Text; TextBox1.Text = myStr.FormatString("#,###"); But it causes error at : Int a = Convert.ToInt16(TextBox1.Text); I want to seperate numbers in textbox anyway. I couldn't use "MaskedTextBox" Please help me. Regards

        0 Offline
        0 Offline
        0x3c0
        wrote on last edited by
        #3

        You can replace FormatString with ToString("N0"). And there are at least two problems with your parsing code. First of all, you convert TextBox1.Text to a 16-bit integer, then store it as a 32-bit integer. This constrains it to the value 65,535. That's far too small for the value you want. Additionally, you're using Convert.ToInt16. Use the method int.Parse() instead.

        Between the motion And the act Falls the Shadow

        1 Reply Last reply
        0
        • F freshonlineMax

          Hello Please help me to show thousand seperator in textbox like: 25000000>25,000,000 I found a formul like below at textchanged event: string myStr = textBox1.Text; TextBox1.Text = myStr.FormatString("#,###"); But it causes error at : Int a = Convert.ToInt16(TextBox1.Text); I want to seperate numbers in textbox anyway. I couldn't use "MaskedTextBox" Please help me. Regards

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, you can tell Parse and TryParse what it is you want to allow, e.g.:

          int val;
          bool OK=int.TryParse("12,345", NumberStyles.AllowThousands, null, out val);
          

          :)

          Luc Pattyn

          :badger: :jig: :badger:

          Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

          :jig: :badger: :jig:

          F 2 Replies Last reply
          0
          • L Luc Pattyn

            Hi, you can tell Parse and TryParse what it is you want to allow, e.g.:

            int val;
            bool OK=int.TryParse("12,345", NumberStyles.AllowThousands, null, out val);
            

            :)

            Luc Pattyn

            :badger: :jig: :badger:

            Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

            :jig: :badger: :jig:

            F Offline
            F Offline
            freshonlineMax
            wrote on last edited by
            #5

            Hello Tanks for your help. I used your code like below : Int64 val; bool ok; ok = Int64.TryParse(TextBox1.Text, NumberStyles.AllowThousands, null, out val); TextBox1.Text = Convert.ToInt64(val.ToString()).ToString("#,###"); The problem accures after typing more than 4 numbers. Because cursor qoes to the left side of numbers, For example i want to type 12345, after typing 4 in typing 5 it TextBox shows : 51,234 Can you help me on this? Regards

            M 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, you can tell Parse and TryParse what it is you want to allow, e.g.:

              int val;
              bool OK=int.TryParse("12,345", NumberStyles.AllowThousands, null, out val);
              

              :)

              Luc Pattyn

              :badger: :jig: :badger:

              Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

              :jig: :badger: :jig:

              F Offline
              F Offline
              freshonlineMax
              wrote on last edited by
              #6

              Hi again I solved it by below code: TextBox1.Select(TextBox1.Text.Length, 0); Bye

              1 Reply Last reply
              0
              • F freshonlineMax

                Hello Tanks for your help. I used your code like below : Int64 val; bool ok; ok = Int64.TryParse(TextBox1.Text, NumberStyles.AllowThousands, null, out val); TextBox1.Text = Convert.ToInt64(val.ToString()).ToString("#,###"); The problem accures after typing more than 4 numbers. Because cursor qoes to the left side of numbers, For example i want to type 12345, after typing 4 in typing 5 it TextBox shows : 51,234 Can you help me on this? Regards

                M Offline
                M Offline
                Morteza Karimian Kelishadrokhi
                wrote on last edited by
                #7

                use this code: by this code,your problem is ok

                private void textBox1_TextChanged(object sender, EventArgs e)
                {
                try
                {
                string ss = textBox1.Text.Replace(",", "");
                int d = int.Parse(ss);
                string gg = String.Format("{0:###,###,###}", d);
                textBox1.Text = gg;

                           textBox1.SelectionStart = textBox1.Text.Length;
                       }
                       catch
                       {
                       }
                
                   }
                

                try { string ss = textBox1.Text.Replace(",", ""); int d = int.Parse(ss); string gg = String.Format("{0:###,###,###}", d); textBox1.Text = gg; textBox1.SelectionStart = textBox1.Text.Length; } catch { }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups