Link to home
Start Free TrialLog in
Avatar of jmsloan
jmsloan

asked on

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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Regards,

DrDamnit
Avatar of jmsloan
jmsloan

ASKER

DrDamnit, that doesn't seem to work so well
Avatar of jmsloan

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial