Results 1 to 17 of 17

Thread: Reducing Text boxes to numbers

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    I need to reduce a text box
    to the number in it.. Excluding
    the letters typed.. thanks!

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Yes I would like to restrict the input to numbers only .. thankyou.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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

  5. #5
    Guest
    Try this:

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If IsNumeric(Chr(KeyAscii)) <> True Then KeyAscii = 0
    End Sub

  6. #6
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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.

  7. #7
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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]

  8. #8
    Guest
    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.

  9. #9
    Guest

    Exclamation Error in my code!

    And also ADD the () after Isnumeric and around the next i or you'll get an error!

    Excuse Me

  10. #10
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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!?!?!?!

  11. #11
    Guest

    Unhappy 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...

  12. #12
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    In text1_change, or where ever you want to put this:
    text1.text = val(text1.text)

  13. #13
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    [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

  14. #14
    Guest
    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.

  15. #15
    New Member
    Join Date
    Oct 2000
    Location
    ÿ§/
    Posts
    2
    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






  16. #16
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  17. #17
    New Member
    Join Date
    Oct 2000
    Location
    ÿ§/
    Posts
    2
    . . . 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
  •  



Click Here to Expand Forum to Full Width