|
-
Mar 30th, 2006, 04:23 AM
#1
Thread Starter
Lively Member
Just quickly...
Is there any way (preferbly non-API) which disallows the user from entering anything other than digits into a standard text box?
Thanks, Zeka
"Most cars on our roads have only one occupant, usually the driver."
- Carol Malia, BBC Anchorwoman
"I do not like this word "bomb." It is not a bomb. It is a device that is exploding."
- Jacques le Blanc, French ambassador on nuclear weapons
"Solutions are not the answer."
- Richard Nixon, former U.S. President
-
Mar 30th, 2006, 04:30 AM
#2
Frenzied Member
Re: Just quickly...
use the keypress event of the textbox
VB Code:
Private Sub txt_Keypress(Keyascii As Integer)
if not (keyascii >= vbKey 0 and Keyascii <= vbkey9) then
keyascii = 0
end if
End Sub
On error goto Trap
Trap:
in case of emergency, drop the case...
****************************************
If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option. if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar
-
Mar 30th, 2006, 04:33 AM
#3
Addicted Member
Re: Just quickly...
Use the textbox Validate event.
VB Code:
Private Sub txtTextBox_Validate(Cancel As Boolean)
If Not IsNumeric(txtTextBox.Text) Then
Cancel = True
End If
End Sub
-
Mar 30th, 2006, 04:35 AM
#4
Re: Just quickly...
This code will take care of paste operations too.
VB Code:
Option Explicit
'to store the lastText that was present in
'the text box before user changed the text
Dim LastText As String
'to check if we are running the Change Event code second time
Dim SecondTime As Boolean
'to store the last cursor positon in the textbox
Dim lastPosition As Long
Private Sub Text1_Change()
'if everything has been deleted or minus was keyed in first
'then don't do anything
If Text1.Text = "" Or Text1.Text = "-" Then
'set the lasttext
LastText = Text1.Text
Exit Sub
End If
'if the method was not called second time
If Not SecondTime Then
'check if the text box is numeric
If Not IsNumeric(Text1.Text) Then
'if not then
SecondTime = True
'set the text to the previous text present in the text box
Text1.Text = LastText
'set the cursor position back to the original
Text1.SelStart = lastPosition
Beep
Else
LastText = Text1.Text
End If
End If
SecondTime = False
End Sub
Private Sub Text1_GotFocus()
'get the last text and store it
LastText = Text1.Text
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'save the last position before
lastPosition = Text1.SelStart
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'don't allow spaces
If KeyAscii = vbKeySpace Then
KeyAscii = 0
Beep
End If
'save the last position before
lastPosition = Text1.SelStart
End Sub
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Mar 30th, 2006, 04:41 AM
#5
Addicted Member
Re: Just quickly...
Shuja Ali
That seems a lot of code just to validate numbers being entered. Surely the Validate event would validate pasted text when the control loses focus? As long as the control about to receive the focus has its CausesValidation property set to True.
-
Mar 30th, 2006, 04:45 AM
#6
Re: Just quickly...
 Originally Posted by davidbishton
Shuja Ali
That seems a lot of code just to validate numbers being entered. Surely the Validate event would validate pasted text when the control loses focus? As long as the control about to receive the focus has its CausesValidation property set to True.
You are right to some extent. But the OP has asked for this
 Originally Posted by Zeka
Is there any way (preferbly non-API) which disallows the user from entering anything other than digits into a standard text box?
Which means that he/she doesn't want theuser to enter anything other than numbers in the textbox.
There is nothing wrond with your way of doing it, but it will allow the use to enter anything in the textbox and when the textbox looses focus it will display the message.
The code that I posted will not allow user to enter anything other than pure umbers and even will not allow user to paste anything that is non-numeric in the textbox. This is the reason why it is so lengthy.
Another easier way would be to use a masked edit control. You literally don't have to write any code in that case.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Mar 30th, 2006, 04:49 AM
#7
Addicted Member
Re: Just quickly...
Ahh yes - I see what it's doing. It's more of a sophisticated way of checking.
No problems.
-
Mar 30th, 2006, 06:57 AM
#8
Re: Just quickly...
or get the numberbox control in the codes section by martin liss .if that could help u lessen ur code !!
__________________
________________0îîî___
___îîî0________(___)____
__(___)_________) _/_____
___\_ (_________(_/______
____\_)_________________
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
|