?
Solved

Formating numbers in a textbox in the TextChanged procedure

Posted on 2004-10-05
3
Medium Priority
?
286 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 800 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 200 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

Industry Leaders: 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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 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