Results 1 to 6 of 6

Thread: How can i only let #'s in a textbox?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    219

    Post

    How can i only let #'s in a textbox?

  2. #2
    Member
    Join Date
    Dec 1999
    Posts
    37

    Post

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If ((KeyAscii >= vbKey0) And (KeyAscii <= vbKey9)) Or KeyAscii = vbKeyBack Then
    Exit Sub
    Else
    KeyAscii = 0
    End If
    End Sub


    Ruchi

  3. #3
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    or try this:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not IsNumeric(Chr(KeyAscii)) Then
            KeyAscii = 0
        End If
    End Sub
    or if you want to have Backspace enabled then use this:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack Then
            KeyAscii = 0
        End If
    End Sub
    It should do the job

    ------------------
    Visual Basic Programmer (at least I want to be one)
    ------------------
    PolComSoft
    You will hear a lot about it.



    [This message has been edited by QWERTY (edited 12-06-1999).]

  4. #4
    Junior Member
    Join Date
    Jun 1999
    Location
    Papillion, NE, USA
    Posts
    21

    Post

    Or for lots of fun, try using the API to do it:

    Private Const GWL_STYLE = (-16)
    Private Const ES_NUMBER = &H2000

    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long


    ' Put this code in your [Form_Load] Event
    Dim Old_Style As Long
    Dim New_Style As Long
    '

    Old_Style = GetWindowLong(Text1.hwnd, GWL_STYLE)
    New_Style = SetWindowLong(Text1.hwnd, GWL_STYLE, Old_Style Or ES_NUMBER)


  5. #5
    Lively Member
    Join Date
    Jun 1999
    Location
    East Anglia, England
    Posts
    73

    Post

    Try using the password char option in the textbox properties

  6. #6
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    PasswordChar wouldn't work, because it changes only appearance of the TextBox, it doesn't change its contest.

    ------------------
    Visual Basic Programmer (at least I want to be one)
    ------------------
    PolComSoft
    You will hear a lot about it.


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