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
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:
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode < 48 Or KeyCode > 57 Then
Text1.Text = Left$(Text1.Text, Len(Text1.Text) - 1)
Text1.SelStart = Len(Text1.Text)
End If
End Sub
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:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'check if value is numeric
If Not IsNumeric(Chr(KeyAscii)) Then
'allow backspace and enter keys only
If Not (KeyAscii = 8 Or KeyAscii = 13) Then
KeyAscii = 0
End If
End If
End Sub
To prevent from pasting you will have to subclass few messages but this will get you started at least.
Re: how prevent user writing letters into a textbox
RhinoBull's way is better
Re: how prevent user writing letters into a textbox
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
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... :confused:
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. :)
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... :confused:
Sample code was posted numerous times by several members. MartinLiss has a link in his signature to (I think) custom Numeric Textbox control.
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
Re: how prevent user writing letters into a textbox
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.
Re: how prevent user writing letters into a textbox
Masked Edit control was already suggested in post #2. Also, not sure of you noticed but he posted in the wrong forum - VB.Net is what he works with.
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).