Question How do I add commas in numbers automatically?

love_lyn99

Member
Joined
Sep 2, 2022
Messages
16
Programming Experience
Beginner
Hi! I am creating a system where when i type a number in textbox1, it automatically add commas. ive searched in the internet and found almost similar functions on what i am looking for. but i have a problem, i dont want the < .00 > decimal to show during i enter the numbers and i will only add decimal if needed.

but some codes i found will only add < .00 > or not able to add decimal at all. here's the code im currently using:

TextBox1_TextChanged:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        TextBox1.Text = CDbl(TextBox1.Text).ToString("n0")
        TextBox1.Select(TextBox1.Text.Length, 0)
End Sub

i did put the <("n0") > because i want to not show the decimal, but instead it completely removes decimal (i think). the "." wont show.

i also tried the ("#,###,###") but it has the same result.

for example: val = 123456
output = 123,456
but if i add decimal: it wont show the "."

i add the 3rd line because i want the cursor to be fix at the end.
 
You previously had this:
VB.NET:
Dim val1 = Val(TextBox1.Text)
and now, for no apparent reason, you have this:
VB.NET:
Dim val1 = TextBox1.Text
That means that val1 is type String and there is no such ToString method on that type. You can't format a String as though it were a number. If you want to format a number then you have to actually have a number to format.
yiiah. about that. ive tried your code. < Dim a = 0.0 Console.WriteLine(a.ToString("#,0.##########")) > but instead of direct value, i changed it to textbox.text and instead of console output i changed it to textbox output. thats why i got the " unable to cast object of string". but if i set the < Dim a = 123.0 > or something, it doesnt give me error. why is that? is it because the textbox1 value is always string? do i have to add command where it convert textbox1 value to int?
 
As I have said, if you want to format a number then you need to have a number. In my example code, I used numbers. The Text of a TextBox is a String, not a number. You need to convert the String to a number first. Val will do that, although it's a bad way to do it.
 
As I have said, if you want to format a number then you need to have a number. In my example code, I used numbers. The Text of a TextBox is a String, not a number. You need to convert the String to a number first. Val will do that, although it's a bad way to do it.
i have a problem with the < Val >. like For example: value = 123456 , the output was = 156. but without val, it doest allow me to insert "." . tried changing it with < Dim val1 As Decimal = TextBox1.Text > but its the same, doesnt allow me to insert "." .like its's disabled or something. i guess ill do the < when enter is pressed, format textbox value. > if i can't find solution for it.
 
I can't see any reason that using Val would do that. It sounds like something is broken somewhere. Please provide step-by-step instructions on how to reproduce that issue and we can see whether we can, indeed, reproduce it.

There's no need to hit Enter. You should probably be handling the Validating and Validated events. The first will allow you to prevent the control losing focus if the contents is not a valid number and the second will allow you to convert the content to a number and then format that as appropriate. The input will then be formatted whenever focus leaves the control. You can still do some work to prevent obviously invalid input, e.g. letters, but tricker stuff can be allowed and validated on leave. That's exactly how a NumericUpDown works.
 
Back
Top