Automatically displaying comma's when typing numbers in VB2008 Express


When I type in a number in a Textbox I would like it to automatically add the comma's.

So if I type 255000.56 it would populate the field as 255,000.56

Below is what I tried. But obviously doesn't do what I am looking for. Thanks in advance.
Private Sub PortfolioAmount_TextChanged(ByVal sender As System.object, By Val e As System.EventArgs) Handles PortfolioAmount.textChanged
End Sub

Open in new window

Who is Participating?
jinalConnect With a Mentor Commented:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

            TextBox1.Text = Double.Parse(TextBox1.Text).ToString("N2")
            TextBox1.Select(TextBox1.Text.Length, 0)
        Catch ex As Exception

        End Try
    End Sub
Wayne Taylor (webtubbs)Connect With a Mentor Commented:
technicaltraderAuthor Commented:
Ok. I am trying the MaskedTextBox. Holy cow! Is there any way to control where the curser lands. I tried every conceivable way to set this up trying to get the number order to correctly populate.

I will spare you all the details but I have changed the "RightToLeft" to "Yes", then tried "No". Played around with writing the mask as ##.###,### and ###,###.## as I changed the input order. I get a result that I would think that I wouldn't each time.

Trying to write example of: 123,456.78  

How can I control the placement of the curser so that it is always on the right no matter if it is placed there manually or tabbed there. Then it writes if I type 12345678 it comes out proper 123,456.78 not 12.345,678 or 87.654,321.

Believe you get the idea. I can't get it to write properly because I can't control the curser placement.

Thank you for telling me about a "MaskedTextBox". Didn't know what that was and will use it in the future for other projects. Increased the points value as well and if this gets more detailed will increase again in order to take care of anyone who chimes in with help.

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

technicaltraderAuthor Commented:
Forgot to change points...
g_johnsonConnect With a Mentor Commented:
on the text changed event you can just do something like this:

textbox1.text = Convert.ToDouble(textbox1.text).ToString("###,###,##0.00")

You could also define a string such as
F = "###,###,##0.00" then replace the formatting above with F
technicaltraderAuthor Commented:
Those suggestions are all working as far as format but It gives me trouble the second I hit a period key. So if I write 12345.66 it throws an Exception. Doesn't want to allow me to enter a period and just end with zero's.

Now this is what I have come up with however I don't know a thing about "LostFocus". Here is how I am handling this at this moment:

Private Sub EntryTextbox_LostFocus(ByVal sender As Object, ByVal e As System.EventsArgs) Handles EntryTestBox.LostFocus
                 EntryTextBox.Text = Format(EntryTextBox.Text, "Currency")
End Sub
Private Sub EntryTextbox_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventsArgs) Handles EntryTestBox.TextChanged

End Sub

This is working very well. I can type 123456.78 in the TextBox and tab to the next box and $123,456.78 format appears in the box. I love this format. Now being that I am using "LostFocus" I don't know how stable this is as I read about "LostFocus". Some of the TextBoxes that I am using this format are writing to a DataSet and saving to a DataBase. So far so good.

Anyone have any comments as to if this is a viable solution or not???

As I go along, I am not sure if I have stated I am wanting to have this write as "Currency" format. Honestly didn't know that is how one would state their wishes until now. My apologies if that bit of information would have changed everything in the solution process.

g_johnsonConnect With a Mentor Commented:
What you have done is a totally valid solution in my opinion
technicaltraderAuthor Commented:
Thank you all for your help. Gave me several ideas to come to my final solution!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.