Results 1 to 6 of 6

Thread: make textbox only input number

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    352

    make textbox only input number

    I have this example:

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 And KeyAscii <> Asc("-") Then KeyAscii = 0
    End Sub
    I know this KeyAscii represents the recently inputted character. But i don't get where it comes from.

    Hmm... i'm not making any sense. Where does this keyascii come from and how does it work? All i'm asking about is the keyascii variable which i don't understand where it comes from and what it is.

  2. #2
    Hyperactive Member Q_Me's Avatar
    Join Date
    Dec 2001
    Posts
    327
    The keycodes for numbers are 48-57. With "0" = Chr$(48) and "9" = Chr$(57)

  3. #3
    Hyperactive Member Q_Me's Avatar
    Join Date
    Dec 2001
    Posts
    327
    ASCII is American Standard Code for Information Interchange. Its what you type on the keyboard. Each key has its own value.

  4. #4
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    When a user presses a key on the textbox, textbox's keypress event is generated.. Operating system.. generates the message and post it to the Textbox with the ascii code of key pressed.. keyascii is the information argument..it shows which key has been pressed.. this comes from the Windows..

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    352
    Hmm... Q_Me... i know what it is and i know about ascii.

    Moinkhan... i liked your explanation but i wasn't exactly trying to explain that.

    Ok... let me try to rephrase the question.

    keyascii is kind of like a constant in visual basics? It always has the last key that was pressed?

    All i really want to know is what goes into the ascii variable. Is it always the last key pressed?

    I will look into examples of it being used @ planetsourcecode later, i have no time now since i have a BIG math test tomorrow!! and a few more quizzes.

  6. #6
    Fanatic Member Wen Lie's Avatar
    Join Date
    Jul 1999
    Location
    Singapore
    Posts
    524
    I got this code from this forum also. Hope the owner doesn't mind for me to post it again here...

    Code:
    'put this in module
    
    Public Function ValidateNumeric(strText As String) As Boolean
        ValidateNumeric = CBool(strText = "" Or strText = "-" Or strText = "-." _
        Or strText = "." Or IsNumeric(strText))
    End Function
    in your form (suppose the object is txtNumeric) :

    Code:
    Private Sub txtNumeric_Change()
        If Not ValidateNumeric(txtNumeric.Text) Then
            txtNumeric.Text = "" 
            'or u can put other statement u want to execute....
        End If
    End Sub

    Hope will help u....


    __________________
    Wille

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