|
-
Jan 2nd, 2007, 01:08 PM
#1
Thread Starter
Hyperactive Member
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
-
Jan 2nd, 2007, 01:24 PM
#2
Member
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
-
Jan 2nd, 2007, 01:27 PM
#3
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.
-
Jan 2nd, 2007, 01:29 PM
#4
Member
Re: how prevent user writing letters into a textbox
RhinoBull's way is better
-
Jan 2nd, 2007, 01:37 PM
#5
Fanatic Member
Re: how prevent user writing letters into a textbox
Live life to the fullest!!
-
Jan 2nd, 2007, 03:35 PM
#6
Thread Starter
Hyperactive Member
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
-
Jan 2nd, 2007, 03:58 PM
#7
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...
-
Jan 2nd, 2007, 04:06 PM
#8
Re: how prevent user writing letters into a textbox
 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.
-
Jan 2nd, 2007, 04:08 PM
#9
Re: how prevent user writing letters into a textbox
 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.
-
Jan 2nd, 2007, 05:16 PM
#10
Thread Starter
Hyperactive Member
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
-
Jan 2nd, 2007, 05:22 PM
#11
Re: how prevent user writing letters into a textbox
-
Jan 3rd, 2007, 08:51 AM
#12
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
-
Jan 3rd, 2007, 08:58 AM
#13
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.
-
Jan 3rd, 2007, 09:34 AM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|