• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

Format a textbox to a decimal as the user types

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?


1 Solution
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
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
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


Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

jmsloanAuthor Commented:
DrDamnit, that doesn't seem to work so well
jmsloanAuthor Commented:
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

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
PAQed with points refunded (50)

EE Admin

Featured Post

Independent Software Vendors: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now