Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm having a problem on how to do display commas after every 3 digits in my textbox. I don't type in the value in the textbox I'm using a button to insert the numbers. Honestly I have read the docs and search the internet on how to do this right but I don't really get it.

I already read this
https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings[^]

Strings.Format(Object, String) Method (Microsoft.VisualBasic) | Microsoft Docs[^]


But I can't get it to work
Here is the code that I'm using to insert the numbers inside the textbox:

VB
Private Sub btnNumbers(sender As Object, e As EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btnDecimal.Click

Dim btnB As Guna.UI.WinForms.GunaButton = DirectCast(sender,Guna.UI.WinForms.GunaButton)


        If txtInput.Text.Length >= txtInput.MaxLength Then


            Exit Sub

        Else

            If txtInput.Text = "0" Or expression Then

                txtInput.Text = ""
                txtInput.Text = btnB.Text
                expression = False

            ElseIf btnB.Text = "." Then

                If Not txtInput.Text.Contains(".") Then

                    txtInput.Text = txtInput.Text + btnB.Text

                End If

            Else
              
                txtInput.Text = txtInput.Text + btnB.Text

            End If


        End If


    End Sub


What I have tried:

And here is the code that I'm trying to use to add commas after 3 digits but I can't get it to work:

VB
txtInput.Text = txtInput.Text + btnB.Text
Dim value As Integer = Convert.ToInt32(txtInput.Text)
txtInput.Text = value.ToString("N0")


After inserting 4 digits like 1,000 it throws an exception: System.FormatException: 'Input string was not in a correct format. The exception is pointing to the line that is bold and have underline.

I'm really lost. I tried to understand the docs but I don't get it
Posted
Updated 22-May-20 1:43am
v2

Convert.ToInt32 won't work if the input string contains the thousands separator.

Try:
VB.NET
Dim value As Ineger = Integer.Parse(txtInput.Text, System.Globalization.NumberStyles.Integer Or System.Globalization.NumberStyles.AllowThousands)
txtInput.Text = value.ToString("N0")
Int32.Parse Method (System) | Microsoft Docs[^]
NumberStyles Enum (System.Globalization) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Maciej Los 22-May-20 8:52am    
5ed!
lelouch_vi 2 22-May-20 9:03am    
Hi Richard Deeming,

Thanks for the solution I'm really amazed by that numberstyle Enum. Now my problem is that the textbox is not accepting decimal value it only accepts whole numbers.
lelouch_vi 2 22-May-20 9:13am    
Here is my code now
 Dim value As Decimal



        If txtInput.Text.Length >= txtInput.MaxLength Then


            Exit Sub

        Else

            If txtInput.Text = "0" Or expression Then

                txtInput.Text = ""
                txtInput.Text = btnB.Text
                expression = False

            ElseIf btnB.Text = "." Then

                If Not txtInput.Text.Contains(".") Then

                    txtInput.Text = txtInput.Text + btnB.Text

                End If

            Else

                txtInput.Text = txtInput.Text + btnB.Text
                value = Decimal.Parse(txtInput.Text, 
                        System.Globalization.NumberStyles.Number Or 
                        System.Globalization.NumberStyles.AllowThousands)
                txtInput.Text = value.ToString("N0")


            End If


        End If
phil.o 22-May-20 10:57am    
NumberStyles.Number already includes NumberStyles.AllowThousands. No need to specify the latter, then.
value = Decimal.Parse(txtInput.Text, System.Globalization.NumberStyles.Number)
Richard Deeming 22-May-20 11:40am    
👍 Good catch.
This is for turning an integer to its string representation using the comma as thousand separator:
C#
int value = 1000;
string representation = value.ToString("#,0"); // representation == "1,000"

And this is to get an integer value from a string using comma as thousand separator:
C#
string representation = "1,000";

// Non-checking version
// Will throw an exception if representation is not a valid integer representation
int value = int.Parse(representation, NumberStyles.AllowThousands);

// Checking version
// Let you handle the case where representation is not a valid integer representation
int value;
if (int.TryParse(representation, NumberStyles.AllowThousands, CultureInfo.InvariantCulture /* or CultureInfo.CurrentCulture */, out value)
{
   // representation is valid
}
else
{
   // representation is invalid
}
 
Share this answer
 
Comments
Maciej Los 22-May-20 8:54am    
5ed!
lelouch_vi 2 22-May-20 9:12am    
Hello phil.o,

I got my code working now using the answer provided by Richard Deeming.
please take a look at my code. I don't why it stop accepting decimal value. It now only accepts whole numbers.

 Dim value As Decimal



        If txtInput.Text.Length >= txtInput.MaxLength Then


            Exit Sub

        Else

            If txtInput.Text = "0" Or expression Then

                txtInput.Text = ""
                txtInput.Text = btnB.Text
                expression = False

            ElseIf btnB.Text = "." Then

                If Not txtInput.Text.Contains(".") Then

                    txtInput.Text = txtInput.Text + btnB.Text

                End If

            Else

                txtInput.Text = txtInput.Text + btnB.Text
                value = Decimal.Parse(txtInput.Text, 
                        System.Globalization.NumberStyles.Number Or 
                        System.Globalization.NumberStyles.AllowThousands)
                txtInput.Text = value.ToString("N0")


            End If


        End If
phil.o 22-May-20 11:00am    
Because you are using N0 as format specifier, which leads to a decimal number with 0 decimal digits. Try to replace N0 with N, for example.
lelouch_vi 2 22-May-20 20:57pm    
hi phil.o,

After playing with the N specifier I think N0 is the best. I tried to use only N and my program only accepts single whole number and then whenever I click other button with value from 1 to 9, it will just add 1 to the right index, it will become like this 1.01... keeps on pressing the button, it will just add 0.01 to the textbox. You have to press a hundred times to make it 2.00.

The adding of commas after every 3 digits is working fine now but inserting a decimal is now the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900