I need to reduce a text box
to the number in it.. Excluding
the letters typed.. thanks!
Printable View
I need to reduce a text box
to the number in it.. Excluding
the letters typed.. thanks!
You are going to have to give a bit more information than that.
Are you wanting to restrict the INPUT to numbers only?
Or was it when you lose focus you wanted to strip out anything BUT numbers?
Yes I would like to restrict the input to numbers only .. thankyou.
Ahah :)
In that case you need to add code into the "KeyPress" Event of the text box.
Restrict the input to KeyAscii values of 48-57 (Numbers 0 to 9) and also include KeyAscii 46 if you want to allow a decimal place.
If the value of KeyAscii is anything BUT those stated above set it to 0 which means nothing will be added.
I would write the code but it would be a good exercise in taking a definition and turning it into something that works
Have fun :D
Try this:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If IsNumeric(Chr(KeyAscii)) <> True Then KeyAscii = 0
End Sub
Yeah I forgot... you need a bit of code in the Change event as well in case someone cuts 'n'pastes text directly into the text box.
Dim!!! Don't forget you cannot strictly use the Text_Change on its own as I could be pressing everything from backspace to back cursor and forward cursor.... I could attempt to insert a character at the beginning or in the middle.
Using the code STRICTLY as you stated above will stuff up my cursor position within the text... Try it and see how badly your code works.
Matthew
Althought that is tight code it also wont allow for decimal places which is valid for numbers. You do however need to also make sure you haven't already pressed the decimal point to determine if you will allow another one.
Oh and you forgot the Text_Change event which needs to be used in conjunction with the KeyPress event so that anyone who Ctrl-V the stuff in there still have it stripped.
I haven't road tested the code but it has the right principle :)Code:
Private Sub Text1_Change
Dim iPos as Integer
Dim NewText as string
For iPos = 1 to Len(Text1.Text)
If NumbersOnly(Text1.Text,Asc(Mid$(Text1.Text,iPos,1)),True) <> 0 Then
NewText = NewText & Mid$(Text1.Text,iPos,1)
End If
Next iPos
Text1.Text = NewText
End Sub
Private Sub Text1_KeyPress(KeyAscii as Long)
KeyAscii = NumbersOnly(Text1.Text, KeyAscii,True)
End Sub
Private Function NumbersOnly(CurrentText as String, KeyAscii as Long, AllowDecimal as Boolean) as Long
Select Case KeyAscii
Case 48 to 57 ' Numbers
NumbersOnly = KeyAscii
Case 46 ' Decimal Point
If AllowDecimal Then
If InStr(CurrentText,".") > 0 then
NumbersOnly = 0
Else
NumbersOnly = KeyAscii
End if
Else
NumbersOnly = 0
End If
Case 8 ' Have to check but I am sure this is backspace
NumbersOnly = KeyAscii
Case Else ' Ignore everything else
NumbersONly = 0
End Select
End Sub
- Can type and delete
- Can type into any section you want
- Can only add 1 decimal point
- Can cut and paste into it
- Restriction in KeyPress Event avoids problems in Change Event
[Edited by Gen-X on 09-18-2000 at 09:29 PM]
It will probably be better if you check the textbox occasionally for numbers as someone might accidently bypass these.
Add a Timer and type the following:
Oh, AND replace Timer1 with your Timer's name and textboxname with your Textbox's name.:)Code:Private Sub Timer1_Timer()
Dim i as String, l as Integer, totaltxt as String
totaltxt = ""
For l = 1 to len(textboxname)
i = mid$(textboxname, i, 1)
if isnumeric i then i = ""
totaltxt = totaltxt & i
Next
textboxname = totaltxt
End Sub
And also ADD the () after Isnumeric and around the next i or you'll get an error!
Excuse Me:(
I'm sorry to say this but adding a timer is the Stupidest Idea I have ever heard!!!
Not only does it increase the processing time of your application but it is doing it on the 1 in a 1,000,000 chance that it somehow manages to get around good code.
You don't add code that functions on a 1 in 1,000,000 chance... you increase your other areas to cope.
How "exactly" is it going to get around both a direct entry into the box and any time the value happens to change!?!?!?!
You may be right.
But after all, msvbvm60 is a dll and, just like how windows can crash, it can crash, too. Some crashes disable a few events and some (most) just crashes. Although, there is only a 1/35,000 chance, it may somehow crash and disable one or two things, in which a timer would ensure it.
But then, your right. Most modern programmers really don't care about the 1 out of quite a lot, but it still can happen, so most of the time it's safe, but there will always be a small chance...
I agree with you, Gen-X, for the most part, timers really take resources, and it may not be worth it...
In text1_change, or where ever you want to put this:
text1.text = val(text1.text)
[code]
'if you don't want the decimal remove the reference to key46
Option Explicit
'allow for backspace and one decimal place
Public x As Integer
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 46 And x < 1 Or _
KeyAscii = 8 Or _
KeyAscii >= 48 And KeyAscii <= 57 Then
KeyAscii = KeyAscii
Else
KeyAscii = 13
End If
If KeyAscii = 46 Then x = 1
End Sub
[code]
You should add:
for Pasting or Dragging.Code:private Sub Textbox_Change()
dim x as string, y as integer, z as string
z = ""
for x = 1 to len(textbox)
x = mid(textbox,x,1)
if isnumeric(x) then
x = ""
beep
end if
z = z & x
next
textbox = z
end sub
Oh, and it's Textbox_Change, not Textbox_Textchange.
Try this - I've made it as generic as possible - Look! no timer!!
Private Sub Text1_KeyPress(KeyAscii As Integer)
NumericOnly KeyAscii, Text1.Text
End Sub
Public Sub NumericOnly(KeyAscii As Integer, TextValue As String)
If (Not IsNumeric(Chr(KeyAscii))) And _
(Chr(KeyAscii) <> ".") Then
KeyAscii = 0
End If
' Allow just one decimal point
If Chr(KeyAscii) = "." And _
(InStr(1, TextValue, ".") > 0) Then
KeyAscii = 0
End If
End Sub
It may be needless to add but I think my code is the shortest :)
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 33 Then Exit Sub
If Chr(KeyAscii) = "." And InStr(1, Text1, ".") > 0 Then KeyAscii = 0
If Not (IsNumeric(Chr(KeyAscii))) And Chr(KeyAscii) <> "." Then KeyAscii = 0
End Sub
. . . But who's is the most readable and reusable ? ?