Results 1 to 7 of 7

Thread: [RESOLVED] Checking Ascii Values

  1. #1

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Resolved [RESOLVED] Checking Ascii Values

    I have a textbox on my form with code in the "keypress" event. In this code it says something like

    VB Code:
    1. If KeyAscii = 13 Then

    Well, how do I get the actual value that is stored in KeyAscii 13. I want to check each keypress to make sure the user typed in a numeric value.

    Something like
    VB Code:
    1. If IsNumeric(KeyAscii.Value) Then

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Checking Ascii Values

    You can return the character of an ascii using CHR$(13) but it won't return a character, as it is the ENDLINE CR sequence.

    I usually place a breakpoint in my code and check what was pressed.

    0 is chr$(48) and 9 is chr$(57)

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Checking Ascii Values

    This should help.
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 Then
    3.     MsgBox "Only numbers are allowed for this entry"
    4.     KeyAscii = 0
    5.     Exit Sub
    6. End If
    7. End Sub
    8.  
    9. 'also, in the change event put this code
    10. 'to prevent pasting in non numeric text
    11. Private Sub Text1_Change()
    12. If Not IsNumeric(Text1.Text) Then
    13.    MsgBox "Only Numbers Are Allowed"
    14.    Text1.Text = vbNullString
    15.    Exit Sub
    16. End If
    17. End Sub

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Checking Ascii Values

    I see Hack posted, but I was talking about this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Text1_KeyPress(KeyAscii As Integer)
    4.   MsgBox KeyAscii & "  " & Chr$(KeyAscii)
    5. End Sub

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Checking Ascii Values

    Quote Originally Posted by The_Grudge
    I have a textbox on my form with code in the "keypress" event. In this code it says something like

    VB Code:
    1. If KeyAscii = 13 Then

    Well, how do I get the actual value that is stored in KeyAscii 13. I want to check each keypress to make sure the user typed in a numeric value.

    Something like
    VB Code:
    1. If IsNumeric(KeyAscii.Value) Then
    VB Code:
    1. If IsNumeric(Chr(KeyAscii)) Then
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: [RESOLVED] Checking Ascii Values

    I think martinliss or Joacim wrote this originally. It only allows numbers to be entered into the text box.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    4. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    5. Private Const GWL_STYLE = (-16)
    6. Private Const ES_NUMBER As Long = &H2000& 'or 8192 in decimal form
    7.  
    8. Private Sub Form_Load()
    9.     Dim style As Long
    10.     Text1.Text = ""
    11.     style = GetWindowLong(Text1.hwnd, GWL_STYLE)
    12.     SetWindowLong Text1.hwnd, GWL_STYLE, style Or ES_NUMBER
    13. End Sub

  7. #7

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: [RESOLVED] Checking Ascii Values

    Thanks Pradeep...that's exactly what I was after.
    Usually Hack helps me out of my jams, good to see a new face. LOL

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