|
-
Sep 18th, 2000, 07:27 PM
#1
Thread Starter
Frenzied Member
I need to reduce a text box
to the number in it.. Excluding
the letters typed.. thanks!
-
Sep 18th, 2000, 07:35 PM
#2
Hyperactive Member
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?
-
Sep 18th, 2000, 07:36 PM
#3
Thread Starter
Frenzied Member
Yes I would like to restrict the input to numbers only .. thankyou.
-
Sep 18th, 2000, 07:44 PM
#4
Hyperactive Member
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
-
Sep 18th, 2000, 07:51 PM
#5
Try this:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If IsNumeric(Chr(KeyAscii)) <> True Then KeyAscii = 0
End Sub
-
Sep 18th, 2000, 07:53 PM
#6
Hyperactive Member
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.
-
Sep 18th, 2000, 08:26 PM
#7
Hyperactive Member
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.
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
I haven't road tested the code but it has the right principle 
- 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]
-
Sep 18th, 2000, 08:38 PM
#8
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:
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
Oh, AND replace Timer1 with your Timer's name and textboxname with your Textbox's name.
-
Sep 18th, 2000, 08:41 PM
#9
Error in my code!
And also ADD the () after Isnumeric and around the next i or you'll get an error!
Excuse Me
-
Sep 19th, 2000, 01:32 AM
#10
Hyperactive Member
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!?!?!?!
-
Sep 19th, 2000, 06:57 PM
#11
Yes, but still a possibility...
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...
-
Sep 19th, 2000, 07:00 PM
#12
So Unbanned
In text1_change, or where ever you want to put this:
text1.text = val(text1.text)
-
Sep 19th, 2000, 07:04 PM
#13
_______
<?>
[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]
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Sep 19th, 2000, 07:11 PM
#14
You should add:
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
for Pasting or Dragging.
Oh, and it's Textbox_Change, not Textbox_Textchange.
-
Oct 25th, 2000, 04:24 PM
#15
New Member
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
-
Oct 25th, 2000, 05:35 PM
#16
Frenzied Member
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
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Oct 27th, 2000, 12:02 PM
#17
New Member
. . . But who's is the most readable and reusable ? ?
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
|