Format a textbox to a decimal as the user types

Posted on 2007-10-11
Last Modified: 2010-04-30
I am using visual basic 6.  I have a textbox called Text1.  As the users type I would like the box to read a formatted decimal number. For instance if the user types a 7 it should format to 0.07, then if the user types a 9 it should format to 0.79, then if the user type another 9 it should format to 7.99.

Does anyone know how to accomplish this?


Question by:jmsloan
    LVL 76

    Expert Comment

    You could consider using a MaskEdit control. Alternatively, you could use the LostFocus event

    Private Sub Text1_LostFocus()
        Text1.Text = Format$(Text1.Text, "#,##0.00")
    End Sub
    LVL 76

    Expert Comment

    Not that it is also possible to use the Change or the KeyPress events.

    These can be confusing for the user who sees the input jumping around in an 'unpredictable' manner
    LVL 32

    Expert Comment

    Hello jmsloan,

    This would work using keyup. IT is a little clunky, but may get you off the to right direction:

    Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
        Static number
        If KeyCode = 27 Then
            number = ""
            Text1.Text = ""
            Exit Sub
        End If
        number = number & Chr(KeyCode)
        Select Case Len(number)
            Case 0
                Text1.Text = ""
            Case 1
                Text1.Text = "0.0" & number
            Case 2
                Text1.Text = "0." & number
            Case Else
                Text1.Text = Left(number, Len(number) - 2) & "." & Right(number, 2)
        End Select
    End Sub


    LVL 3

    Author Comment

    DrDamnit, that doesn't seem to work so well
    LVL 3

    Author Comment

    After some messing with some code this code seems to work

    Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
       Dim i As Integer
       Dim x As Long

    If ((KeyCode >= 48 And KeyCode <= 57) Or (KeyCode >= 96 And KeyCode <= 105) Or (KeyCode = 8)) And Len(Text1.Text) < 10 Then
       For i = 1 To Len(Text1.Text)
           If Mid(Text1.Text, i, 1) = "." Then
               x = x
               x = x & Mid(Text1.Text, i, 1)
           End If
    psngCurNumber = Val(x) / 100

    Text1.Text = psngCurNumber
    SendKeys "{END}"
    End If
    LVL 76

    Expert Comment


    Not sure if you are happy with your code. I can't actually get it to work.

    See if this is close to what you want. It uses a command button to clear the text box for a new entry.

    Option Explicit
    Dim s As Integer

    Private Sub cmdNewNumber_Click()
    Text1.Text = ""
    s = 0
    End Sub

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim c As Integer
        Select Case KeyAscii
            Case 48 To 57
                c = KeyAscii - 48
                KeyAscii = 0
        End Select
        Text1.Text = Val(Text1.Text) + c * 10 ^ (s - 2)
        s = s + 1
    End Sub
    LVL 1

    Accepted Solution

    PAQed with points refunded (50)

    EE Admin

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    745 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

    18 Experts available now in Live!

    Get 1:1 Help Now