Results 1 to 6 of 6

Thread: Text Field, Integer only?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2002
    Location
    Munich
    Posts
    25

    Question Text Field, Integer only?

    Hi there,

    is there any possibility to restrict input of a text field to integers and one point only (so that you are able to display decimals e.g. 0.23).

    Does someone know how to do so?

    Thx
    Charly

  2. #2
    champgary
    Guest
    CHECK THE ASCII IN TEXTBOX'S CHANGE EVENT AND KEYPRESS EVENT
    VB Code:
    1. Private Sub txtCPin_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii = 8 Then Exit Sub
    3.     If Not (KeyAscii > 47 And KeyAscii < 58) Then    
    4.     If Not (KeyAscii =46) Then
    5.             KeyAscii = 0
    6.     end if
    7.     end if

    Gary

  3. #3
    Hyperactive Member
    Join Date
    May 2002
    Location
    Chicago
    Posts
    271
    Or...
    Code:
    Private Sub Text1_Change()
      If Text1.Text = "" Then Exit Sub
      If Not IsNumeric(Text1.Text) Then
        MsgBox "Please enter a number only"
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
      End If
    End Sub
    Sometimes what you're looking for is exactly where you left it.

  4. #4
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819
    or use a masked edit box if your version of VB came with it.

  5. #5
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Code:
    Private Sub Text1_Change()
      If Text1.Text = "" Then Exit Sub
      If Not IsNumeric(Text1.Text) Or InStr(1, Text1.Text, ".") > 0 Then
        MsgBox "Please enter an integer number only"
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
      End If
    End Sub
    <removed by admin>

  6. #6
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii <> vbKeyBack And KeyAscii <> vbKeyReturn And KeyAscii <> vbKeyEscape Then
    3.         If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
    4.             If KeyAscii = Asc(".") Then
    5.                 If InStr(1, Text1.Text, ".") <> 0 Then
    6.                     KeyAscii = 0
    7.                 End If
    8.             Else
    9.                 KeyAscii = 0
    10.             End If
    11.         End If
    12.     End If
    13. End Sub

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

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