question

jewel-5023 avatar image
0 Votes"
jewel-5023 asked JiachenLiMFST-9349 answered

How to do this if i want to see the decimal value of the text box with 3 digit grouping and also the negative value.

I want decimal value in the text in the text box. I did a lot of searching and got the code that I got.
In this code only numbers can be given but decimal values ​​cannot be given. For example, I want to write: 10,000.75 or if I want to give a negative value like: -25,752.6, it is not like that.
I would have benefited if an experienced person had wasted his precious time and cooperated in the solution. Thanks
Thanks in advance to the experienced collaborators.


 Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
     If (Me.TextBox1.Text.Length > 0) Then
         Dim numeric As Decimal = CDec(Me.TextBox1.Text)
         Me.TextBox1.Text = String.Format("{0:#,#}", numeric)
         Me.TextBox1.SelectionStart = Me.TextBox1.Text
     End If

 End Sub


 Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
     If (e.KeyChar < "0"c OrElse "9"c < e.KeyChar) AndAlso (e.KeyChar <> ControlChars.Back) Then
         e.Handled = True
     End If


 End Sub
dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JiachenLiMFST-9349 avatar image
0 Votes"
JiachenLiMFST-9349 answered

Hi @jewel-5023 ,
You can use Strings.FormatNumber(Object, Int32, TriState, TriState, TriState) Method to add thousands separators to numbers.

 TextBox3.Text = Strings.FormatNumber(numeric, 2, -1, , -1)

Best Regards.
Jiachen Li


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.