Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Automatically displaying comma's when typing numbers in VB2008 Express

Posted on 2009-07-02
8
Medium Priority
?
461 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 48

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 400 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 15

Accepted Solution

by:
jinal earned 800 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 800 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 800 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

963 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