Announcement

Collapse
No announcement yet.

InputBox$ and the cancel button

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

  • InputBox$ and the cancel button

    This should be simple but I can find no information via search or in POFFS

    If I use an inputbox$, how do I know if the user pressed the cancel button?
    (Checking the length of the string is no good because I set a default value)

    Do I have to make my own custom input dialog so that I can check which button was pressed?

    should be a simple answer, but I am guessing I will have to go custom?

    ------------------
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    Cliff,

    Couldn't you just compare the default string before and
    after? If it isn't the same, then the user changed something.
    If it is the same, perhaps he hit cancel. Havn't tried it,
    that's just my guess.

    ------------------
    Fred
    "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"
    Fred
    "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

    Comment


    • #3
      Just tried this and it seems to work

      Code:
      #Compile Exe
      #Dim All
      
      Function PBMain() As Long
        Local strText As String, strResult As String
        
        strText="December"
        strResult=InputBox$("Enter A Month",,strText)
        If Len(strResult) Then
           MsgBox("User Entered " & strResult)
        Else
           MsgBox("User Cancelled!")
        End If
        
        PBMain=0
      End Function
      ------------------
      Fred
      "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"
      Fred
      "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

      Comment


      • #4
        If you need to know if he canceled rather than simply 'did not changed the default,' perhaps INPUTBOX$ is not the optimal choice of functions here.
        Code:
              Prompt  [editable default]
            
                  <OK>   <Cancel>
        I think that's about a ten-minute DDT dialog job....

        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          It seems the InputBox$ function returns an empty string
          when the Cancel button is clicked, even if a default string
          is in the entry field. That makes sense kind of. Just tried
          it with my code above.

          ------------------
          Fred
          "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"
          Fred
          "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

          Comment


          • #6
            You are right. Dummy me staring at the same block of code so long that until
            I came home and took a break, it was then obvious that if cancel or
            the x to close, causes the returned string to be nothing.

            My other problem was a check to verify that the value of the string would not be
            zero (and obviously the value of nothing is zero)

            Thanks to all

            ------------------
            Engineer's Motto: If it aint broke take it apart and fix it

            "If at 1st you don't succeed... call it version 1.0"

            "Half of Programming is coding"....."The other 90% is DEBUGGING"

            "Document my code????" .... "WHYYY??? do you think they call it CODE? "

            Comment


            • #7
              if cancel or the x to close, causes the returned string to be nothing.
              That sure doesn't sound like the 'correct' behavior if one supplies a 'default' value.

              Maybe that should be called an "initial" value instead of a "default" value.

              Regardless, this behavior should documented.

              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Empty string with ascii code -1?

                Originally posted by Fred Harris View Post
                It seems the InputBox$ function returns an empty string
                when the Cancel button is clicked, even if a default string
                is in the entry field. (...)
                That's what I've always thought. But... if you query for the string's ascii code, -1 is returned. Can somebody explain this? I like to know if this is a safe method to verify if the user pressed the cancel button.

                Code:
                '  run this code and press the Cancel button to see that an empty string
                '  returns an ascii value of minus one
                
                #COMPILE EXE
                #DIM ALL
                
                FUNCTION PBMAIN () AS LONG
                  LOCAL sResult AS STRING
                
                  sResult = INPUTBOX$("First Name", "Test Inputbox$", "Egbert")
                  MSGBOX "String content: " & sResult & $CRLF & _
                         "String length: " & FORMAT$(LEN(sResult)) & $CRLF & _
                         "Ascii code: " & FORMAT$(ASC(sResult)), 64, "Test Inputbox$"
                END FUNCTION

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

                Comment


                • #9
                  Code:
                    sResult = ""
                    MsgBox "Ascii code: " & FORMAT$(ASC(sResult)), 64, "Test Ascii code"
                  "If the string passed is null (zero-length) or the position is zero or greater than the length of the string, the value -1 is returned".
                  Rgds, Dave

                  Comment


                  • #10
                    Originally posted by Dave Biggs View Post
                    "If the string passed is null (zero-length) or the position is zero or greater than the length of the string, the value -1 is returned".
                    Position? Position of what?

                    Apart from that, a zero-length string obviously returns ascii -1. That's exactly what I said in my previous message. What I want to know is whether it's a documented feature or not.

                    LATER: Sorry, didn't read the help file very well. It is documented. See: ASC in Keyword Reference.
                    Last edited by Egbert Zijlema; 7 Jan 2009, 03:38 AM. Reason: RTFM

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

                    Comment


                    • #11
                      LATER: Sorry, didn't read the help file very well. It is documented. See: ASC in Keyword Reference.
                      The apology should be made by me - I didn't make it clear I was quoting the help file - nor which part.
                      Rgds, Dave

                      Comment


                      • #12
                        Safe wrapper in SC forum now

                        Given some incertainties throughout this thread, especially in respect with the cancel button, I've tried to create a "safe" wrapper for Power Basic's INPUTBOX$ function. Although it is a string function, my wrapper function also validates a "numbers only" setting.

                        You'll find it here: http://www.powerbasic.com/support/pb...221#post306221

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

                        Comment


                        • #13
                          >validates a "numbers only" setting
                          Code:
                            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
                          ...
                          ===>
                          Code:
                          IF Istrue (iNumerals) THEN
                              IF ISTRUE VERIFY(sResult, "0123456789") THEN 
                          ...

                          The PB VERIFY function is kind of funky... it returns FALSE (zero) on a "good" verification. Probably why it's often overlooked for this kind of editing.

                          The "plus offset" is, you get the position of the first offending character.

                          Or, in COBOL....
                          Code:
                            IF sResult IS NOT NUMERIC THEN 
                           ...
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            Originally posted by Michael Mattias View Post
                            >validates a "numbers only" setting
                            (...)
                            The PB VERIFY function is kind of funky... it returns FALSE (zero) on a "good" verification. Probably why it's often overlooked for this kind of editing.
                            (...)
                            The "plus offset" is, you get the position of the first offending character.
                            (...)
                            Well, PB's VERIFY function is not the only one. Quite a lot of Windows API functions return %ERROR_SUCCESS (= 0) if ending successful. What you call the "plus offset" is completely irrelevant in this routine. When one of the characters is invalid (don't mind which one), my routine simply re-runs to give the user a new chance.

                            So, I do not exactly understand your message. Did you just want to point me to an equal-worthy alternative or to a "far better solution"?

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

                            Comment


                            • #15
                              I think it's equal.

                              VERIFY giveth position of offending character and does not require explicit MID$/ASC code; VERIFY taketh away what seems more natural to me:
                              Code:
                                IF ISTRUE VERIFY (myString, "0123456789") THEN 
                                    MSGBOX "All characters of mystring are numeric digits"
                                .....
                              Semantic, I know, but it is what "feels" correct to me.

                              MCM
                              Michael Mattias
                              Tal Systems (retired)
                              Port Washington WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment


                              • #16
                                I need some help to create a dialog like window so I can add 2 textboxes
                                to find a file name and then search within that file
                                I would like to have a second dialog popup called from the first dialog or inputbox$
                                i could not find a place to start a new topic
                                thanks
                                James Morgan

                                Comment


                                • #17
                                  Each section has a "+ New Topic" button.

                                  In the Samples\DDT\FileFind directory where PB is installed is code to search for a file. That can be modified for the second dialog.

                                  (copy the files before changing, the samples will stay original)

                                  Cheers,
                                  Dale

                                  Comment


                                  • #18
                                    Egbert's updated link to Safe wrapper for INPUTBOX$ in Source Code.

                                    Comment


                                    • #19
                                      Originally posted by James C Morgan View Post
                                      I need some help to create a dialog like window so I can add 2 textboxes
                                      to find a file name and then search within that file
                                      I would like to have a second dialog popup called from the first dialog or inputbox$
                                      i could not find a place to start a new topic
                                      thanks
                                      James Morgan
                                      New topic HERE
                                      https://forum.powerbasic.com/forum/u...box#post788054
                                      The world is strange and wonderful.*
                                      I reserve the right to be horrifically wrong.
                                      Please maintain a safe following distance.
                                      *wonderful sold separately.

                                      Comment

                                      Working...
                                      X