Results 1 to 14 of 14

Thread: how prevent user writing letters into a textbox

  1. #1

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Question how prevent user writing letters into a textbox

    Hello !

    Could someone telle me how to prevent a user to write letters into a textbox (to allow only numbers). In general, I would like to solve this, how to define what a user my type into a textbox (Forms.TextBox).

    Thanks a lot for any feedback.

    Regards,
    Fabian

  2. #2
    Member
    Join Date
    Dec 2006
    Posts
    53

    Re: how prevent user writing letters into a textbox

    This isn't perfect (if the end user starts pressing multiple buttons it will mess up), but it's a start... The easier method might be doing a search for "Masked Edit Control" or "Masked Text Box"...
    VB Code:
    1. Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    2.   If KeyCode < 48 Or KeyCode > 57 Then
    3.  
    4.     Text1.Text = Left$(Text1.Text, Len(Text1.Text) - 1)
    5.     Text1.SelStart = Len(Text1.Text)
    6.    
    7.   End If
    8.  
    9. End Sub
    Help me win cash, vote for my code: Random Quotes - Setting / Retrieving Cookies

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

    Re: how prevent user writing letters into a textbox

    The following will prevent users from typing non-numeric values, however they can still paste it:
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     'check if value is numeric
    3.     If Not IsNumeric(Chr(KeyAscii)) Then
    4.         'allow backspace and enter keys only
    5.         If Not (KeyAscii = 8 Or KeyAscii = 13) Then
    6.             KeyAscii = 0
    7.         End If
    8.     End If
    9. End Sub
    To prevent from pasting you will have to subclass few messages but this will get you started at least.

  4. #4
    Member
    Join Date
    Dec 2006
    Posts
    53

    Re: how prevent user writing letters into a textbox

    RhinoBull's way is better
    Help me win cash, vote for my code: Random Quotes - Setting / Retrieving Cookies

  5. #5
    Fanatic Member newprogram's Avatar
    Join Date
    Apr 2006
    Location
    in your basement
    Posts
    769

    Re: how prevent user writing letters into a textbox

    Live life to the fullest!!

  6. #6

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Re: how prevent user writing letters into a textbox

    Hello all,

    I can not believe that there is no way to limit input characters without need to code. In the most simpliest programming studios you have such ways. Like telling an "allowedCharacters" property "0-9" (which would only allow numbers) or "0.0" wich will force a number with one digit befor and one after the dot etc.

    Certainly the validate methode makes all possible, but it would be so much easier as I told.

    Regards,
    Fabian

  7. #7
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: how prevent user writing letters into a textbox

    There's also an easy API method that sets the TextBox to numeric only. I don't remember who posted the code...bushmobile I think...

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

    Re: how prevent user writing letters into a textbox

    Quote Originally Posted by fabianus
    Hello all,

    I can not believe that there is no way to limit input characters without need to code...
    Unfortunately in VB6 there isn't any property that you can specify so you pretty much on your own.

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

    Re: how prevent user writing letters into a textbox

    Quote Originally Posted by DigiRev
    There's also an easy API method that sets the TextBox to numeric only. I don't remember who posted the code...bushmobile I think...
    Sample code was posted numerous times by several members. MartinLiss has a link in his signature to (I think) custom Numeric Textbox control.

  10. #10

    Thread Starter
    Hyperactive Member fabianus's Avatar
    Join Date
    Sep 2004
    Location
    Paris
    Posts
    402

    Re: how prevent user writing letters into a textbox

    Oh guys, excuse-me so much. I posted in the wrong forum ! I need the answer for .NET ! :-(

    Excuse me.

    Regards,
    Fabian

  11. #11

  12. #12
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: how prevent user writing letters into a textbox

    You could also use a masked edit textbox instead of the standard textbox and set the mask to all numbers.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  13. #13

  14. #14
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: how prevent user writing letters into a textbox

    I didn't notice that in Post #2 but is is still avail in .Net (I use it regularly to ensure numbers only).
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

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