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
Solved

Formating numbers in a textbox in the TextChanged procedure

Posted on 2004-10-05
3
274 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - KeyPress Event 4 38
VB.Net - TypeInitializer Error 25 32
Footer for each row on Gridview 2 32
VB.net Duplicating a table - primary key not created 3 31
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…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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