Solved

Automatically displaying comma's when typing numbers in VB2008 Express

Posted on 2009-07-02
8
450 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

22 Experts available now in Live!

Get 1:1 Help Now