Solved

Automatically displaying comma's when typing numbers in VB2008 Express

Posted on 2009-07-02
8
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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