C# UWP how to type a number string and get commas real time in order?












0















I'm trying to type numbers into a textbox and have it add a comma after every third number in real time. I need to convert the string of numbers into a real number because I need to do a simple math equation in real time as well. A problem I'm running into is that if I type in order of 1234567 after I hit 4 it adds the comma and then the input box jumps to the beginning of the string. So I type from 1 to 7 I get 5,671,234



private void PriceBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
if (!String.IsNullOrEmpty(PriceBox.Text))
{
int x = Int32.Parse(PriceBox.Text, NumberStyles.AllowThousands);
float y = x * .50f;
Half.Text = y.ToString("N0");
PriceBox.Text = x.ToString("N0");
}
}









share|improve this question



























    0















    I'm trying to type numbers into a textbox and have it add a comma after every third number in real time. I need to convert the string of numbers into a real number because I need to do a simple math equation in real time as well. A problem I'm running into is that if I type in order of 1234567 after I hit 4 it adds the comma and then the input box jumps to the beginning of the string. So I type from 1 to 7 I get 5,671,234



    private void PriceBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
    {
    if (!String.IsNullOrEmpty(PriceBox.Text))
    {
    int x = Int32.Parse(PriceBox.Text, NumberStyles.AllowThousands);
    float y = x * .50f;
    Half.Text = y.ToString("N0");
    PriceBox.Text = x.ToString("N0");
    }
    }









    share|improve this question

























      0












      0








      0








      I'm trying to type numbers into a textbox and have it add a comma after every third number in real time. I need to convert the string of numbers into a real number because I need to do a simple math equation in real time as well. A problem I'm running into is that if I type in order of 1234567 after I hit 4 it adds the comma and then the input box jumps to the beginning of the string. So I type from 1 to 7 I get 5,671,234



      private void PriceBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
      {
      if (!String.IsNullOrEmpty(PriceBox.Text))
      {
      int x = Int32.Parse(PriceBox.Text, NumberStyles.AllowThousands);
      float y = x * .50f;
      Half.Text = y.ToString("N0");
      PriceBox.Text = x.ToString("N0");
      }
      }









      share|improve this question














      I'm trying to type numbers into a textbox and have it add a comma after every third number in real time. I need to convert the string of numbers into a real number because I need to do a simple math equation in real time as well. A problem I'm running into is that if I type in order of 1234567 after I hit 4 it adds the comma and then the input box jumps to the beginning of the string. So I type from 1 to 7 I get 5,671,234



      private void PriceBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
      {
      if (!String.IsNullOrEmpty(PriceBox.Text))
      {
      int x = Int32.Parse(PriceBox.Text, NumberStyles.AllowThousands);
      float y = x * .50f;
      Half.Text = y.ToString("N0");
      PriceBox.Text = x.ToString("N0");
      }
      }






      c# uwp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 7:29









      luigiluigi

      1012




      1012
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You can format a number to have commas after every 3 digit as described here. One way is to hook the TextChanged event, convert the current number to a comma separated number, and replace the current text with the comma separated number.



          Also, to stop ovetflow exception, you have to unsubscribe from the TextChanged event and then subscribe again.



          The total event handler is here:



          private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
          {
          TextBox textBox = sender as TextBox;

          /// unsubscribe, so that the replacing doesn't invoke this handler again
          textBox.TextChanged -= TextBox_TextChanged;
          if(double.TryParse(textBox.Text, out double value))
          {
          textBox.Text = value.ToString("N0");
          }

          /// put the cursor in the end of the text
          textBox.Select(textBox.Text.Length, 0);

          /// subscribe again
          textBox.TextChanged += TextBox_TextChanged;
          }


          Hope that helps.



          Edit:
          To allow numeric values only, hook the PreviewKeyDown event, allow numeric keys only. like this:



          private void TextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
          {
          bool proceed =
          (e.Key >= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9) ||
          (e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9);
          e.Handled = !proceed;
          }


          And at last, to allow more than 3 commas, I have edited the code, it should allow more than 3 commas now (basically, I replaced int with double).






          share|improve this answer


























          • Thank you for the reply. I'm at work right now, but I'll give it a try as soon as I get home. If it works I'll be sure to upvote you and mark as correct answer

            – luigi
            Jan 1 at 22:18








          • 1





            Thank you very much. Your code worked. It does up to 3 commas and then it stops. It also allows letters and special characters, but I'll try to figure that out. Thanks again

            – luigi
            Jan 2 at 7:44











          • I have edited the answer to meet the additional requirements too. Check it.

            – Muzib
            Jan 2 at 8:08











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993771%2fc-sharp-uwp-how-to-type-a-number-string-and-get-commas-real-time-in-order%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You can format a number to have commas after every 3 digit as described here. One way is to hook the TextChanged event, convert the current number to a comma separated number, and replace the current text with the comma separated number.



          Also, to stop ovetflow exception, you have to unsubscribe from the TextChanged event and then subscribe again.



          The total event handler is here:



          private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
          {
          TextBox textBox = sender as TextBox;

          /// unsubscribe, so that the replacing doesn't invoke this handler again
          textBox.TextChanged -= TextBox_TextChanged;
          if(double.TryParse(textBox.Text, out double value))
          {
          textBox.Text = value.ToString("N0");
          }

          /// put the cursor in the end of the text
          textBox.Select(textBox.Text.Length, 0);

          /// subscribe again
          textBox.TextChanged += TextBox_TextChanged;
          }


          Hope that helps.



          Edit:
          To allow numeric values only, hook the PreviewKeyDown event, allow numeric keys only. like this:



          private void TextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
          {
          bool proceed =
          (e.Key >= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9) ||
          (e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9);
          e.Handled = !proceed;
          }


          And at last, to allow more than 3 commas, I have edited the code, it should allow more than 3 commas now (basically, I replaced int with double).






          share|improve this answer


























          • Thank you for the reply. I'm at work right now, but I'll give it a try as soon as I get home. If it works I'll be sure to upvote you and mark as correct answer

            – luigi
            Jan 1 at 22:18








          • 1





            Thank you very much. Your code worked. It does up to 3 commas and then it stops. It also allows letters and special characters, but I'll try to figure that out. Thanks again

            – luigi
            Jan 2 at 7:44











          • I have edited the answer to meet the additional requirements too. Check it.

            – Muzib
            Jan 2 at 8:08
















          2














          You can format a number to have commas after every 3 digit as described here. One way is to hook the TextChanged event, convert the current number to a comma separated number, and replace the current text with the comma separated number.



          Also, to stop ovetflow exception, you have to unsubscribe from the TextChanged event and then subscribe again.



          The total event handler is here:



          private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
          {
          TextBox textBox = sender as TextBox;

          /// unsubscribe, so that the replacing doesn't invoke this handler again
          textBox.TextChanged -= TextBox_TextChanged;
          if(double.TryParse(textBox.Text, out double value))
          {
          textBox.Text = value.ToString("N0");
          }

          /// put the cursor in the end of the text
          textBox.Select(textBox.Text.Length, 0);

          /// subscribe again
          textBox.TextChanged += TextBox_TextChanged;
          }


          Hope that helps.



          Edit:
          To allow numeric values only, hook the PreviewKeyDown event, allow numeric keys only. like this:



          private void TextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
          {
          bool proceed =
          (e.Key >= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9) ||
          (e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9);
          e.Handled = !proceed;
          }


          And at last, to allow more than 3 commas, I have edited the code, it should allow more than 3 commas now (basically, I replaced int with double).






          share|improve this answer


























          • Thank you for the reply. I'm at work right now, but I'll give it a try as soon as I get home. If it works I'll be sure to upvote you and mark as correct answer

            – luigi
            Jan 1 at 22:18








          • 1





            Thank you very much. Your code worked. It does up to 3 commas and then it stops. It also allows letters and special characters, but I'll try to figure that out. Thanks again

            – luigi
            Jan 2 at 7:44











          • I have edited the answer to meet the additional requirements too. Check it.

            – Muzib
            Jan 2 at 8:08














          2












          2








          2







          You can format a number to have commas after every 3 digit as described here. One way is to hook the TextChanged event, convert the current number to a comma separated number, and replace the current text with the comma separated number.



          Also, to stop ovetflow exception, you have to unsubscribe from the TextChanged event and then subscribe again.



          The total event handler is here:



          private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
          {
          TextBox textBox = sender as TextBox;

          /// unsubscribe, so that the replacing doesn't invoke this handler again
          textBox.TextChanged -= TextBox_TextChanged;
          if(double.TryParse(textBox.Text, out double value))
          {
          textBox.Text = value.ToString("N0");
          }

          /// put the cursor in the end of the text
          textBox.Select(textBox.Text.Length, 0);

          /// subscribe again
          textBox.TextChanged += TextBox_TextChanged;
          }


          Hope that helps.



          Edit:
          To allow numeric values only, hook the PreviewKeyDown event, allow numeric keys only. like this:



          private void TextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
          {
          bool proceed =
          (e.Key >= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9) ||
          (e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9);
          e.Handled = !proceed;
          }


          And at last, to allow more than 3 commas, I have edited the code, it should allow more than 3 commas now (basically, I replaced int with double).






          share|improve this answer















          You can format a number to have commas after every 3 digit as described here. One way is to hook the TextChanged event, convert the current number to a comma separated number, and replace the current text with the comma separated number.



          Also, to stop ovetflow exception, you have to unsubscribe from the TextChanged event and then subscribe again.



          The total event handler is here:



          private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
          {
          TextBox textBox = sender as TextBox;

          /// unsubscribe, so that the replacing doesn't invoke this handler again
          textBox.TextChanged -= TextBox_TextChanged;
          if(double.TryParse(textBox.Text, out double value))
          {
          textBox.Text = value.ToString("N0");
          }

          /// put the cursor in the end of the text
          textBox.Select(textBox.Text.Length, 0);

          /// subscribe again
          textBox.TextChanged += TextBox_TextChanged;
          }


          Hope that helps.



          Edit:
          To allow numeric values only, hook the PreviewKeyDown event, allow numeric keys only. like this:



          private void TextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
          {
          bool proceed =
          (e.Key >= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9) ||
          (e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9);
          e.Handled = !proceed;
          }


          And at last, to allow more than 3 commas, I have edited the code, it should allow more than 3 commas now (basically, I replaced int with double).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 8:07

























          answered Jan 1 at 8:24









          MuzibMuzib

          1,094719




          1,094719













          • Thank you for the reply. I'm at work right now, but I'll give it a try as soon as I get home. If it works I'll be sure to upvote you and mark as correct answer

            – luigi
            Jan 1 at 22:18








          • 1





            Thank you very much. Your code worked. It does up to 3 commas and then it stops. It also allows letters and special characters, but I'll try to figure that out. Thanks again

            – luigi
            Jan 2 at 7:44











          • I have edited the answer to meet the additional requirements too. Check it.

            – Muzib
            Jan 2 at 8:08



















          • Thank you for the reply. I'm at work right now, but I'll give it a try as soon as I get home. If it works I'll be sure to upvote you and mark as correct answer

            – luigi
            Jan 1 at 22:18








          • 1





            Thank you very much. Your code worked. It does up to 3 commas and then it stops. It also allows letters and special characters, but I'll try to figure that out. Thanks again

            – luigi
            Jan 2 at 7:44











          • I have edited the answer to meet the additional requirements too. Check it.

            – Muzib
            Jan 2 at 8:08

















          Thank you for the reply. I'm at work right now, but I'll give it a try as soon as I get home. If it works I'll be sure to upvote you and mark as correct answer

          – luigi
          Jan 1 at 22:18







          Thank you for the reply. I'm at work right now, but I'll give it a try as soon as I get home. If it works I'll be sure to upvote you and mark as correct answer

          – luigi
          Jan 1 at 22:18






          1




          1





          Thank you very much. Your code worked. It does up to 3 commas and then it stops. It also allows letters and special characters, but I'll try to figure that out. Thanks again

          – luigi
          Jan 2 at 7:44





          Thank you very much. Your code worked. It does up to 3 commas and then it stops. It also allows letters and special characters, but I'll try to figure that out. Thanks again

          – luigi
          Jan 2 at 7:44













          I have edited the answer to meet the additional requirements too. Check it.

          – Muzib
          Jan 2 at 8:08





          I have edited the answer to meet the additional requirements too. Check it.

          – Muzib
          Jan 2 at 8:08




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993771%2fc-sharp-uwp-how-to-type-a-number-string-and-get-commas-real-time-in-order%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          Npm cannot find a required file even through it is in the searched directory

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith