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. Decimal format in the textbox Text Change C# (1000000 -> 1,000,000)

Decimal format in the textbox Text Change C# (1000000 -> 1,000,000)

Scheduled Pinned Locked Moved C#
csharphelptutoriallearning
10 Posts 4 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.
  • N Offline
    N Offline
    nhanlaptrinh
    wrote on last edited by
    #1

    Have you asked yourself in the currency format function TextChange how my example below the cursor does not focus on the No. 2 spot, the book help me thanks! if (textBox1.Text == "") return; else { int iIndex = textBox1.Text.IndexOf('.'); if (iIndex == -1) { } else { string strT = textBox1.Text.Substring(iIndex + 1, 1); if (textBox1.Text != "") { } } double a = double.Parse(textBox1.Text.Trim(',')); textBox1.Text = a.ToString("#,###"); }

    OriginalGriffO B P N 4 Replies Last reply
    0
    • N nhanlaptrinh

      Have you asked yourself in the currency format function TextChange how my example below the cursor does not focus on the No. 2 spot, the book help me thanks! if (textBox1.Text == "") return; else { int iIndex = textBox1.Text.IndexOf('.'); if (iIndex == -1) { } else { string strT = textBox1.Text.Substring(iIndex + 1, 1); if (textBox1.Text != "") { } } double a = double.Parse(textBox1.Text.Trim(',')); textBox1.Text = a.ToString("#,###"); }

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Sorry, but that doesn't make much sense. When you post code, please use the code widget above the textbox - it preserves formatting which makes the whole thing more readable. I have reformatted your code, and also refactored it to make it more readable - taking out redundant conditions and so forth:

              if (textBox1.Text != "")
                  {
                  int iIndex = textBox1.Text.IndexOf('.');
                  if (iIndex != -1)
                      {
                      string strT = textBox1.Text.Substring(iIndex + 1, 1);
                      }
                  double a = double.Parse(textBox1.Text.Trim(','));
                  textBox1.Text = a.ToString("#,###");
                  }
      

      The trouble is that I have no idea what you are trying to do, so I also have no idea what problem you are trying to get help with! Please try to explain a bit better so we can help you.

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      N 2 Replies Last reply
      0
      • N nhanlaptrinh

        Have you asked yourself in the currency format function TextChange how my example below the cursor does not focus on the No. 2 spot, the book help me thanks! if (textBox1.Text == "") return; else { int iIndex = textBox1.Text.IndexOf('.'); if (iIndex == -1) { } else { string strT = textBox1.Text.Substring(iIndex + 1, 1); if (textBox1.Text != "") { } } double a = double.Parse(textBox1.Text.Trim(',')); textBox1.Text = a.ToString("#,###"); }

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        Not sure what you want to do. Perhaps:

        int amount;
        if (int.TryParse(textBox1.Text, out amount))
        {
        textBox1.Text=amount.ToString("#,###");
        }

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Sorry, but that doesn't make much sense. When you post code, please use the code widget above the textbox - it preserves formatting which makes the whole thing more readable. I have reformatted your code, and also refactored it to make it more readable - taking out redundant conditions and so forth:

                  if (textBox1.Text != "")
                      {
                      int iIndex = textBox1.Text.IndexOf('.');
                      if (iIndex != -1)
                          {
                          string strT = textBox1.Text.Substring(iIndex + 1, 1);
                          }
                      double a = double.Parse(textBox1.Text.Trim(','));
                      textBox1.Text = a.ToString("#,###");
                      }
          

          The trouble is that I have no idea what you are trying to do, so I also have no idea what problem you are trying to get help with! Please try to explain a bit better so we can help you.

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

          N Offline
          N Offline
          nhanlaptrinh
          wrote on last edited by
          #4

          We now want to enter the number into the textbox, textbox itself decimal format and displays the time on that textbox. For example, enter "123456789", the textbox will appear as 123,456,789. I have a problem when entering a string from 1 to 9 are ok, but when inserting a number in the range from 1 to 9, the textbox back to position the cursor on the first position, thanks to your help with , thank you very much. Code I wrote

          private void textBox1_TextChanged(object sender, EventArgs e)
          {
          try
          {
          if (textBox1.Text != "")
          {
          int iIndex = textBox1.Text.IndexOf('.');
          if (iIndex != -1)
          {
          string strT = textBox1.Text.Substring(iIndex + 1, 1);
          }
          double a = double.Parse(textBox1.Text.Trim(','));
          if (textBox1.SelectionStart >= textBox1.Text.Length)
          {
          textBox1.Text = a.ToString("#,###");
          textBox1.SelectionStart = textBox1.Text.Length;
          }
          else
          {
          textBox1.Text = a.ToString("#,###");
          //Help?
          }
          }
          }
          catch (Exception) { MessageBox.Show("Must enter the number ."); }
          }

          B 1 Reply Last reply
          0
          • N nhanlaptrinh

            We now want to enter the number into the textbox, textbox itself decimal format and displays the time on that textbox. For example, enter "123456789", the textbox will appear as 123,456,789. I have a problem when entering a string from 1 to 9 are ok, but when inserting a number in the range from 1 to 9, the textbox back to position the cursor on the first position, thanks to your help with , thank you very much. Code I wrote

            private void textBox1_TextChanged(object sender, EventArgs e)
            {
            try
            {
            if (textBox1.Text != "")
            {
            int iIndex = textBox1.Text.IndexOf('.');
            if (iIndex != -1)
            {
            string strT = textBox1.Text.Substring(iIndex + 1, 1);
            }
            double a = double.Parse(textBox1.Text.Trim(','));
            if (textBox1.SelectionStart >= textBox1.Text.Length)
            {
            textBox1.Text = a.ToString("#,###");
            textBox1.SelectionStart = textBox1.Text.Length;
            }
            else
            {
            textBox1.Text = a.ToString("#,###");
            //Help?
            }
            }
            }
            catch (Exception) { MessageBox.Show("Must enter the number ."); }
            }

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            Do not use the TextChanged event, LostFocus or Validating are better.

            N 1 Reply Last reply
            0
            • N nhanlaptrinh

              Have you asked yourself in the currency format function TextChange how my example below the cursor does not focus on the No. 2 spot, the book help me thanks! if (textBox1.Text == "") return; else { int iIndex = textBox1.Text.IndexOf('.'); if (iIndex == -1) { } else { string strT = textBox1.Text.Substring(iIndex + 1, 1); if (textBox1.Text != "") { } } double a = double.Parse(textBox1.Text.Trim(',')); textBox1.Text = a.ToString("#,###"); }

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Mightn't a NumericUpDown be what you want?

              N 1 Reply Last reply
              0
              • B Bernhard Hiller

                Do not use the TextChanged event, LostFocus or Validating are better.

                N Offline
                N Offline
                nhanlaptrinh
                wrote on last edited by
                #7

                I think there are workarounds, but they have not thought out.

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  Mightn't a NumericUpDown be what you want?

                  N Offline
                  N Offline
                  nhanlaptrinh
                  wrote on last edited by
                  #8

                  Have you no one has any solution to make stars? :(

                  1 Reply Last reply
                  0
                  • N nhanlaptrinh

                    Have you asked yourself in the currency format function TextChange how my example below the cursor does not focus on the No. 2 spot, the book help me thanks! if (textBox1.Text == "") return; else { int iIndex = textBox1.Text.IndexOf('.'); if (iIndex == -1) { } else { string strT = textBox1.Text.Substring(iIndex + 1, 1); if (textBox1.Text != "") { } } double a = double.Parse(textBox1.Text.Trim(',')); textBox1.Text = a.ToString("#,###"); }

                    N Offline
                    N Offline
                    nhanlaptrinh
                    wrote on last edited by
                    #9

                    This big forum who can solve the problem of your own?

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Sorry, but that doesn't make much sense. When you post code, please use the code widget above the textbox - it preserves formatting which makes the whole thing more readable. I have reformatted your code, and also refactored it to make it more readable - taking out redundant conditions and so forth:

                              if (textBox1.Text != "")
                                  {
                                  int iIndex = textBox1.Text.IndexOf('.');
                                  if (iIndex != -1)
                                      {
                                      string strT = textBox1.Text.Substring(iIndex + 1, 1);
                                      }
                                  double a = double.Parse(textBox1.Text.Trim(','));
                                  textBox1.Text = a.ToString("#,###");
                                  }
                      

                      The trouble is that I have no idea what you are trying to do, so I also have no idea what problem you are trying to get help with! Please try to explain a bit better so we can help you.

                      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                      N Offline
                      N Offline
                      nhanlaptrinh
                      wrote on last edited by
                      #10

                      Thanks for your interest in my question the last few days, now I have done the above issue then. :)

                      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