Results 1 to 7 of 7

Thread: limiting the decimal point to 1

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    limiting the decimal point to 1

    I find that most code to limit the decimal point to one is written similar to this.

    If KeyAscii = 46 And InStr(Text1, ".") > 0 Then
    KeyAscii = 0
    Exit Sub
    or
    Case 8, 48 To 57, 46 And InStr(TextBox, ".") = 0

    The downside is that there must be a integer before the decimal point for it to function. Is there no way to limit the decimal point to one AND have the decimal point be the first digit entered? An example would be something less than 1 like .625

    I have tried all that I know so far to no avail. Any ideas?

    Thanks
    Last edited by Davek1; Aug 25th, 2006 at 09:41 AM.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: limiting the decimal point to 1

    Here is another approach - it's not perfect (just a quick sample) so you may need to work with code written in the Text1_Change() evnt handler:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const GWL_STYLE = (-16)
    4. Private Const ES_NUMBER = &H2000&
    5.  
    6. Private Declare Function GetWindowLong _
    7.     Lib "user32" Alias "GetWindowLongA" _
    8.     (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    9.  
    10. Private Declare Function SetWindowLong _
    11.     Lib "user32" Alias "SetWindowLongA" _
    12.     (ByVal hwnd As Long, ByVal nIndex As Long, _
    13.     ByVal dwNewLong As Long) As Long
    14.  
    15. Private Sub Form_Load()
    16. '========================
    17. Dim curStyle&, newStyle&
    18.  
    19.     'allow numeric entries only
    20.     Text1.Text = ""
    21.     curStyle = GetWindowLong(Text1.hwnd, GWL_STYLE) Or ES_NUMBER
    22.     newStyle = SetWindowLong(Text1.hwnd, GWL_STYLE, curStyle)
    23.    
    24. End Sub
    25.  
    26. Private Sub Text1_Change()
    27.     If IsNumeric(Text1.Text) Then
    28.         Text1.Text = Format(Text1.Text, "0.0")
    29.     End If
    30. End Sub

  3. #3

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: limiting the decimal point to 1

    Using KeyPress it would be
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. Dim lPos As Long
    3.  
    4.     lPos = InStr(Text1.Text, ".")
    5.     Select Case KeyAscii
    6.         Case Asc("0") To Asc("9")
    7.             If lPos And Text1.SelStart >= lPos And Len(Text1.Text) - lPos > 0 Then
    8.                 KeyAscii = 0
    9.             End If
    10.         Case Asc(".")
    11.             If lPos Then KeyAscii = 0
    12.         Case Else
    13.             If KeyAscii <> vbKeyBack Then KeyAscii = 0
    14.     End Select
    15. End Sub
    Last edited by jcis; Aug 25th, 2006 at 09:17 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    12

    Re: limiting the decimal point to 1

    JCIS,
    For an input of .625, I still have to enter an integer first. like this > 0.625
    I want to avoid the first 0 as it is not a natural thing to input.

    I am working with an array and have had to add "index as integer" to the first line to get it to function. Same holds true for the other code variations.

    VB Code:
    1. Private Sub text1_KeyPress(index As Integer, KeyAscii As Integer)

    Maybe this "index as integer" makes it not work as intended?

    Thanks
    Dave
    Last edited by Davek1; Aug 25th, 2006 at 10:36 AM.

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: limiting the decimal point to 1

    I see you are using a control array, but im not sure if I understand what you want, this will allow 1 decimal separator and the number must start with (.)
    VB Code:
    1. Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
    2. Dim lPos As Long
    3.  
    4.     lPos = InStr(Text1.Item(Index).Text, ".")
    5.     Select Case KeyAscii
    6.         Case Asc("0") To Asc("9")
    7.             If Len(Text1.Item(Index).Text) = 0 Then KeyAscii = 0
    8.         Case Asc(".")
    9.             If lPos Then KeyAscii = 0
    10.         Case Else
    11.             If KeyAscii <> vbKeyBack Then KeyAscii = 0
    12.     End Select
    13. End Sub

  7. #7
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    396

    Re: limiting the decimal point to 1

    Have you thought of using a masked edit box?

Posting Permissions

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



Click Here to Expand Forum to Full Width