how to add commas after 3 digit in vb.net?

  • Thread starter Thread starter lelouch_vi
  • Start date Start date
L

lelouch_vi

Guest
Hello,

I'm having a problem on how to do display commas after every 3 digits in my textbox. I don't typed 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 Custom numeric format strings and this String format I'm already frustrated about this.

Here is the code that I'm using to insert the numbers inside the textbox:

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



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

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 jeez really frustrating.

Continue reading...
 
Back
Top