Solved

Formating numbers in a textbox in the TextChanged procedure

Posted on 2004-10-05
3
276 Views
Last Modified: 2012-08-14
I need help in formating numbers that the user inputs in a TextBox.  I want as the user inputs a number in the textbox it will format the number in real time. If they type in 1 (1) then 0 (10) then 0 (100) then another 0 it will look like this 1,000 (the comma) I tryed the the following code but the curser seems to end up at the front of the number  once I reach the first comma and you can not enter any more digits

 Private Sub txtNum_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNum.TextChanged
        txtNum.Text = Format(CLng(txtNum.Text), "#,#")
    End Sub

Thanks....
0
Comment
Question by:ucla11
[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
  • 2
3 Comments
 
LVL 8

Expert Comment

by:bramsquad
ID: 12228708
Private Sub txtNum_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNum.TextChanged
        txtNum.Text = CDbl(txtNum.Text).ToString("N0")
End Sub

~b
0
 
LVL 8

Accepted Solution

by:
bramsquad earned 200 total points
ID: 12228756
if you want the cursor to stay at the end, use this code

Private Sub txtNum_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNum.TextChanged
        txtNum.Text = CDbl(txtNum.Text).ToString("N0")
        txtNum.SelectionStart = txtNum.TextLength
End Sub

~b
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
ID: 12228942
Just one addition to bramsquads code.  Put it in a Try...Catch or your app will crash if what is in the text box does convert properly to a Long:

    Private Sub txtNum_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNum.TextChanged
        Try
            txtNum.Text = CLng(txtNum.Text).ToString("N0")
            txtNum.SelectionStart = txtNum.TextLength
        Catch ex As Exception
        End Try
    End Sub

~IM
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to duplicate form in Visual Studio 2015 2 98
Asp.Net Session Question 2 36
visual studio vb.net windows onkeyup 2 30
Need help with a query 14 40
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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