Is there any way (preferbly non-API) which disallows the user from entering anything other than digits into a standard text box?
Thanks, Zeka :)
Printable View
Is there any way (preferbly non-API) which disallows the user from entering anything other than digits into a standard text box?
Thanks, Zeka :)
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
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
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
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 thisQuote:
Originally Posted by davidbishton
Which means that he/she doesn't want theuser to enter anything other than numbers in the textbox.Quote:
Originally Posted by Zeka
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. :)
Ahh yes - I see what it's doing. It's more of a sophisticated way of checking.
No problems.
or get the numberbox control in the codes section by martin liss .if that could help u lessen ur code !!