i have one textbox how would i make that textbox that user can't type other then number not word's also spacebar and backspace work but user can't type just alphabatic word. does any one know how to do this
Printable View
i have one textbox how would i make that textbox that user can't type other then number not word's also spacebar and backspace work but user can't type just alphabatic word. does any one know how to do this
Don't use a text box. Used a MaskedEdit Box. You can then put in the mask property something like this... ##### and it will only allow the user to enter numbers. The maskededit control is in your components list.
thank you but i knew about makededit but i don't want that i want to lurn with textbox becaue i am beganner, but thanks
Use the keydown event to check what they have typed and then put in code to block it. Their is a parameter in the keydown event that tells you what key is being pressed and then a value to let you cancel the key. Ill put up some code if you want.
You have funny names, are you Japanese?
Just kidding, here's a shorter way, using 2 API functions:
Code:'This project needs a TextBox
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 = &H2000&
Public Sub SetNumber(NumberText As TextBox, Flag As Boolean)
Dim curstyle As Long, newstyle As Long
'retrieve the window style
curstyle = GetWindowLong(NumberText.hwnd, GWL_STYLE)
If (Flag) Then
curstyle = curstyle Or ES_NUMBER
Else
curstyle = curstyle And (Not ES_NUMBER)
End If
'Set the new style
newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle)
'refresh
NumberText.Refresh
End Sub
Private Sub Form_Load()
SetNumber Text1, True
Me.Caption = "Now, try typing some letters into the textbox"
End Sub
Insert the following code in your TextBox. It will filter out A-z.
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) Like "[A-z]" Then KeyAscii = 0
End Sub
Code:'Check for a Numeric Character, Excluding Backspace and Space. Verified with VB6.
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not(IsNumeric(Chr(KeyAscii))) And KeyAscii <> 32 And KeyAscii <> 8 Then KeyAscii = 0
End Sub