|
-
Feb 4th, 2000, 08:52 AM
#1
Thread Starter
Lively Member
I want a text box to accept only numbers(0-9) and only x ammount of them like 99999999 then no more are allowed hope this makes sense
later,
Iceman
-
Feb 4th, 2000, 09:01 AM
#2
Hyperactive Member
To limit the amount of character that are entered in a textbox, just set the MaxLength property.
As for limiting the type of character put something like this in the KeyPress event of the textbox
Code:
If KeyAscii <> vbKey0 Or KeyAscii <> vbKeyNumpad0 Or ... Then
Text1.Text = Mid(Text1.Text, 0, Len(Text1.Text) - 1)
End If
------------------
Ryan
-
Feb 4th, 2000, 09:42 AM
#3
Thread Starter
Lively Member
There has GOT to be a better way to do that
-
Feb 4th, 2000, 11:06 AM
#4
Fanatic Member
In the KeyPress event you could put:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
And I don't think there is better way to do it. Email me if I'm wrong.
HTH
------------------
Visual Basic Programmer
------------------
PolComSoft
You will hear a lot about it.
-
Feb 4th, 2000, 04:29 PM
#5
New Member
Here's what I use:
This way it's dynamic and you can use it all over by typing one line of code.
The validate string value is to be set to what ever character you wish the user to enter.
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = ValiText(KeyAscii, "0123456789", True)
End Sub
Public Function ValiText(KeyIn As Integer, _
ValidateString As String, Editable As Boolean) As Integer
Dim ValidateList As String
Dim KeyOut As Integer
If Editable = True Then
ValidateList = UCase(ValidateString) & Chr(8)
Else
ValidateList = UCase(ValidateString)
End If
If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
KeyOut = KeyIn
Else
KeyOut = 0
Beep
End If
ValiText = KeyOut
End Function
------------------
-
Feb 4th, 2000, 04:35 PM
#6
New Member
Then as far as the amount of the numbers entered just set the Maxlenth = to the desire number of digits you wish to see.
This is assuming your using a textbox, Richtextbox or any edit box with that property
-
Apr 6th, 2005, 12:20 AM
#7
PowerPoster
Re: Limiting input type in a textbox....how?
I know you can specify the maxlength, but can you specify a min length for a text field?
-
Apr 6th, 2005, 12:56 AM
#8
Re: Limiting input type in a textbox....how?
One way to enforce a minimum length would be to use the Validate event
VB Code:
Private Sub Text1_Validate(Cancel As Boolean)
If Len(Text1.Text) < 5 Then
MsgBox "Must be at least 5 digits"
Cancel = True
End If
End Sub
And another way to enforce numeric entry in a text box
VB Code:
Option Explicit
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = -16
Const ES_NUMBER As Long = &H2000&
Public Sub SetToNumeric(UText As TextBox, Flag As Boolean)
'
' Allow only numeric input in TextBox
'
' Flag = True - allow numeric only
' = False - allow all
'
' Note: This doesn't prevent pasting alpha into the textbox
'
Dim curstyle As Long
Dim newstyle As Long
curstyle = GetWindowLong(UText.hwnd, GWL_STYLE)
If Flag Then
curstyle = curstyle Or ES_NUMBER
Else
curstyle = curstyle And (Not ES_NUMBER)
End If
newstyle = SetWindowLong(UText.hwnd, GWL_STYLE, curstyle)
UText.Refresh
End Sub
So to set a textbox to allow numbers only, you would do:
SetToNumeric myTextBox, True
and to change it back to allow anything to be entered:
SetToNumeric myTextBox, False
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Apr 6th, 2005, 01:23 AM
#9
PowerPoster
Re: Limiting input type in a textbox....how?
You can also use Isnumeric() to limit the entry to numeric only
VB Code:
If IsNumeric(Text1.Text) = False Then
MsgBox "Type numeric value", , "Result"
Text1.SetFocus
Exit Sub
End If
-
Apr 6th, 2005, 04:04 AM
#10
Re: Limiting input type in a textbox....how?
Hang on, has no one thought of using the masked edit box? It acts just like a text box, and you can mask the input so it acts exactl how you wants it to.
Project > Components > Select "Microsoft Masked Edit Control 6.0" then click ok.
Then set the mask to 999999999
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
|