Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Save wrapper for INPUTBOX$, including test for "numerals only"

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Save wrapper for INPUTBOX$, including test for "numerals only"

    See also discussion at: http://www.powerbasic.com/support/pb...ad.php?t=22185

    Code:
    '  INPBOX.BAS
    '  "safe" wrapper for INPUTBOX$ function
    '  includes validation for "digits only"
    '  status: Public Domain
    '  coded by Egbert Zijlema
    
    #COMPILE EXE
    #DIM ALL
    
    %FALSE              = 0
    %TRUE               = 1
    %IDCANCEL           = 2
    %IDRETRY            = 4
    %MB_RETRYCANCEL     = &H00000005&
    %MB_ICONEXCLAMATION = &H00000030&
    
    FUNCTION iInputBox(sPrompt AS STRING, sTitle AS STRING, sResult AS STRING, OPTIONAL BYVAL sDefault AS STRING, OPTIONAL BYVAL iNumerals AS LONG) AS LONG
      ' iNumerals can either be %FALSE (= 0 or ignored) or %TRUE
      ' if iNumerals is set while sDefault is not, sDefault - although optional - cannot be ignored. It should be passed as an empty string ("")
      LOCAL iPos AS LONG, iAgain AS LONG
    
      RETRY:
      FUNCTION = %TRUE
      sResult = INPUTBOX$(sPrompt, sTitle, sDefault)
      IF sResult = "" THEN                                ' cancel button clicked
        FUNCTION = %FALSE
        EXIT FUNCTION
      END IF
    
      IF ISTRUE(iNumerals) THEN                           ' flag "digits only" is set
        FOR iPos = 1 TO LEN(sResult)
          SELECT CASE AS LONG ASC(sResult, iPos)
            CASE < 48, > 57
              iAgain = MSGBOX ("Numbers only, please", %MB_RETRYCANCEL OR %MB_ICONEXCLAMATION, " Invalid input!")
              SELECT CASE AS LONG iAgain
                CASE %IDRETRY
                  sResult = ""                            ' reset result
                  GOTO RETRY
                CASE %IDCANCEL
                  FUNCTION = %FALSE
                  EXIT FUNCTION
              END SELECT
          END SELECT
        NEXT
      END IF
    END FUNCTION
    
    FUNCTION PBMAIN() AS LONG
      LOCAL sResult AS STRING
    
      ' first test: input normal text, no predefined default
      IF ISTRUE iInputBox("Your name:", " 1st Test INPUTBOX$", sResult) THEN
        MSGBOX "You typed: " & sResult, 64, " 1st Test INPUTBOX$"
      ELSE
        EXIT FUNCTION
      END IF
    
      ' second test: numerals only, with default - all parameters passed
      IF ISTRUE iInputBox("Input a year", " 2nd Test INPUTBOX$", sResult, "2010", %TRUE) THEN
        MSGBOX "You typed: " & sResult, 64, " 2nd Test INPUTBOX$"
      ELSE
        EXIT FUNCTION
      END IF
    
      ' third test: numerals only, no default
      IF ISTRUE iInputBox("Input a number", " 3d Test INPUTBOX$", sResult, "", %TRUE) THEN
        MSGBOX "You typed: " & sResult, 64, " 3d Test INPUTBOX$"
      ELSE
        EXIT FUNCTION
      END IF
    END FUNCTION
    Last edited by Egbert Zijlema; 7 Jan 2009, 09:38 AM. Reason: Spelling "Safe" in Title

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***
Working...
X