Search code examples
c#textboxmessagebox

How to format a number 1000 to 1,000 for textbox to use in a Tostring in C# in a label and in a message.show


How to format a number 1000 to 1,000 for textbox to use in a Tostring in C# in a label and in a message.show

myString = "Attempt #" + AttemptNumber.ToString( );

AttemptsLbl.Text = AttemptNumber.ToString();

MessageBox.Show(" Match Found for All 3 Digits - it took " + AttemptNumber + " tries!");


Solution

  • With this format #,##0, which can be used in either ToString or String.Format. When inserting the number in the middle of a string, I find String.Format more convenient. Both examples below.

    myString = String.Format("Attempt #{0:#,##0}", AttemptNumber);
    AttemptsLbl.Text = AttemptNumber.ToString("#,##0");
    MessageBox.Show(String.Format("Match Found for All 3 Digits - it took {0:#,##0} tries!", AttemptNumber));
    

    The format string from this Q&A: https://stackoverflow.com/a/295821/832052