Only One Decimal Point In Textbox, Using Keyascii-VBForums
Results 1 to 7 of 7

Thread: Only One Decimal Point In Textbox, Using Keyascii

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    50

    Only One Decimal Point In Textbox, Using Keyascii

    This is want I have at the moment.

    Code:
    Private Sub text1_keypress(keyascii As Integer)
            Select Case keyascii
                Case "8", "46", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "127"
            Case Else
                keyascii = 0
            End Select
    End Sub
    But I only want a maximum of one decimal Point, how do I do this?

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Only One Decimal Point In Textbox, Using Keyascii

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 46 Then
        If InStr(1, Text1.Text, ".") > 0 Then
           KeyAscii = 0
           MsgBox "Multiple decimal points are not allowed.", vbOKOnly + vbInformation, "Input Error"
           Exit Sub
        End If
    End If
    End Sub
    Last edited by Hack; Jan 31st, 2008 at 01:40 PM.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3
    New Member VB-Nerd's Avatar
    Join Date
    Jan 2008
    Location
    KS
    Posts
    8

    Re: Only One Decimal Point In Textbox, Using Keyascii

    'you can also try something like this....

    Private Sub Text1_Change()
    Static strOldValue As String

    If IsNumeric(Text1.Text) Then
    strOldValue = Text1.Text
    Else
    'Text1.Text = 0
    Text1.Text = strOldValue ' or a default value like 0
    End If

    Debug.Print Text1.Text
    End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    50

    Re: Only One Decimal Point In Textbox, Using Keyascii

    Thanks

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,355

    Re: Only One Decimal Point In Textbox, Using Keyascii

    Not sure why you have quotes around the KeyAscii values. Anyway, this code appears to work well:
    Code:
    Dim PointLook As Boolean
    
    Private Sub Text1_Change()
    If InStr(Text1.Text, Chr$(46)) = 0 Then PointLook = False
    End Sub
    
    Private Sub Text1_Keypress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 8, 46, 127, 48 To 57
            If PointLook = True And KeyAscii = 46 Then
                KeyAscii = 0
            ElseIf KeyAscii = 46 Then PointLook = True
            End If
        Case Else
            KeyAscii = 0
        End Select
    End Sub
    Note that backspace can erase the decimal, so the Text1.Change event toggles the Boolean such that if the decimal is erased, it is available once again.
    Doctor Ed

  6. #6

  7. #7
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,168

    Re: Only One Decimal Point In Textbox, Using Keyascii

    This code will also allow the minus sign if the first charcter
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    numbersOnly KeyAscii, Text1
    End Sub

    Sub numbersOnly(KeyAscii As Integer, txtBox As TextBox)
    On Error GoTo e
    'Allow Backspace, minus sign, Period and Enterkey
    If KeyAscii <> 8 And KeyAscii <> 45 And KeyAscii <> 46 And KeyAscii <> 13 Then
    If Chr(KeyAscii) < "0" Or Chr(KeyAscii) > "9" Then
    KeyAscii = 0
    Beep
    End If

    End If

    If InStr(txtBox, ".") Then 'don't allow more than 1 decimal
    If KeyAscii = 46 Then KeyAscii = 0
    GoTo bail
    End If

    If Len(txtBox.Text) > 0 And KeyAscii = 45 Then 'only allow minus sign at Leftmost position
    KeyAscii = 0
    End If
    Exit Sub
    e:
    MsgBox "numbersOnly" & err.number & " " & err.Description
    bail:
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.