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?

thanks,

jmsloan
LVL 3
jmsloanAsked:
Who is Participating?
 
Computer101Commented:
PAQed with points refunded (50)

Computer101
EE Admin
0
 
GrahamSkanRetiredCommented:
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
0
 
GrahamSkanRetiredCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
DrDamnitCommented:
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

Regards,

DrDamnit
0
 
jmsloanAuthor Commented:
DrDamnit, that doesn't seem to work so well
0
 
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
       Else
           x = x & Mid(Text1.Text, i, 1)
       End If
   Next
   
psngCurNumber = Val(x) / 100

Text1.Text = psngCurNumber
SendKeys "{END}"
   
End If
0
 
GrahamSkanRetiredCommented:
jmsloan,

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
Text1.SetFocus
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.