Solved

Formating numbers in a textbox in the TextChanged procedure

Posted on 2004-10-05
3
263 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

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

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

11 Experts available now in Live!

Get 1:1 Help Now