Solved

Formating numbers in a textbox in the TextChanged procedure

Posted on 2004-10-05
3
270 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
  • 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 85

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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 …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 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