Solved

Automatically displaying comma's when typing numbers in VB2008 Express

Posted on 2009-07-02
8
451 Views
Last Modified: 2013-11-27
Hi.

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
 
 

String.Format("###,###,###.##")
 

End Sub

Open in new window

0
Comment
Question by:technicaltrader
8 Comments
 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 100 total points
ID: 24767638
0
 

Author Comment

by:technicaltrader
ID: 24768409
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.

0
 

Author Comment

by:technicaltrader
ID: 24768420
Forgot to change points...
0
 
LVL 15

Accepted Solution

by:
jinal earned 200 total points
ID: 24769069
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Try

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

        End Try
    End Sub
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Assisted Solution

by:g_johnson
g_johnson earned 200 total points
ID: 24770696
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
0
 

Author Comment

by:technicaltrader
ID: 24773490
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.

0
 
LVL 4

Assisted Solution

by:g_johnson
g_johnson earned 200 total points
ID: 24775293
What you have done is a totally valid solution in my opinion
0
 

Author Closing Comment

by:technicaltrader
ID: 31599317
Thank you all for your help. Gave me several ideas to come to my final solution!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now