• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Inputbox with drop down list question

Scheduled Pinned Locked Moved Developers' Forum
10 Posts 3 Posters 2.9k Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    Frankn
    last edited by 11 Aug 2011, 08:01

    Hi,

    I'm trying to get a drop down list to work. I'm able to creat an input box and even add a drop down list. But what I'm not able to figure out and can't seem to find the answer too is how to associate a value or have code run from the drop down list options.

    As you might have guessed I'm new at this whole programming thing. 😄 Any help would be appreciated.

    Thanks,
    Frank

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 11 Aug 2011, 10:12

      @frankn said:

      ... how to associate a value or have code run from the drop down list options.

      What your talking about, is called (in programming,) a "callback" method. Sorry to say, that the API does not implement callbacks for inputboxes. (You can do them, but you must write a extension in C/C++ and use the native system calls.)

      In the Google Ruby API, all you can do is collect the values chosen in the the results array. And then use conditional statements depending on what they are.

      If the user cancels the inputbox, results will be negative (either false or nil,) and that's why it is common practice to follow the inputbox line with a conditional block that tests that results eval as true. (Remember in Ruby everything evals true, except false and nil.):

      results = UI.inputbox( ...args... )
      if results
        # process the user's choices
      end
      

      Jim Foltz wrote an wrapper that makes using inputboxes more "Rubyish"
      get it here, at his website:
      http://sketchuptips.blogspot.com/2008/03/inputbox-class.html

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • D Offline
        Dan Rathbun
        last edited by 11 Aug 2011, 10:26

        Also there is a way to put an inputbox inside a 'loop' so if an input error occurs, the box is redisplayed.

        Look at TheDro's Voxelize plugin, I helped him set his inputbox up that way.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • T Offline
          TIG Moderator
          last edited by 11 Aug 2011, 10:36

          prompts=["Choose Color: ", "Enter Number: "]
          values=["Red", 0] ### defaults are always 'Red' and 0
          colors=["Red", "Green", "Blue"]
          colors_joined=colors.join("|")

          which gives "Red|Green|Blue"

          drops=[colors_joined, ""]

          note how in 'drops' you list the dropdown[s] and a "" for non-dropdown[s] like 'number'

          title="RGB?"
          results=UI.inputbox(prompts, values, drops, title)
          return nil if not results

          this is to stop if the user clicks 'Cancel'...

          color=results[0]
          number=results[1]

          alternatively you can get them in one go

          from the array thus:

          color,number = results

          IF you want to reuse the last entered values as the defaults for the next time the dialog opens in that session use

          @color="Red" if not @color
          @number=0 if not @number

          then

          values=[@color, @number]
          ###and after you get good 'results'
          @color,@number = results

          ###You can then use tests on the values to decide what you do next...
          if @color=="Red"

          do this...

          elsif @color=="Green"

          do that...

          else ### it must be blue

          do the other...

          end
          OR
          case @color
          when "Red"

          do this...

          ###etc
          end

          If you want to test 'Yes|No|Maybe' or 'True|False' or '0|1|2|3' remember that the result is s a 'string'.
          So var1=true if @var1=="True" to make a boolean variable or use var2=@var2.to_i to covert input into a 'number' choice...

          As Dan says if you will only accept >=0 as a direct number input then make the dialog run from a nested method

          def dialog()...
          ###then in tour main code have
          return nil if not self.dialog()

          the def dialog() method contains these lines

          after the dialog has opened etc...

          ...
          return nil if not results
          ...
          if results[1] < 0
          UI.messagebox("The number must be >= 0 !")
          self.dialog()

          retry - user either then cancels >> nil or enter a number >= 0 >> true

          else ### number was OK
          @number=results[1] ### reset @number only now...
          return true
          end

          at the end

          🤓

          TIG

          1 Reply Last reply Reply Quote 0
          • F Offline
            Frankn
            last edited by 11 Aug 2011, 18:52

            Thank you guys that helped a ton in my understanding of how that works!

            I do have a few of questions...

            What is the the differnce in creating a list this way...

            list = ["item1"|"item2"|"item3"]

            and this...

            list = ["Wall", "Wall - Corner", "Base", "Base - Corner"]
            list_joined = list.join("|")
            drops = [list_joined, ""]

            any advantages disadvantages?

            Also in some examples that I found the don't use the UI in the inputbox command... again any advantage in doing either or?

            And lastly I'm having trouble understanding/implementing this last part...

            def dialog()...
            ###then in tour main code have
            return nil if not self.dialog()

            the def dialog() method contains these lines

            after the dialog has opened etc...

            ...
            return nil if not results
            ...
            if results[1] < 0
            UI.messagebox("The number must be >= 0 !")
            self.dialog()

            retry - user either then cancels >> nil or enter a number >= 0 >> true

            else ### number was OK
            @number=results[1] ### reset @number only now...
            return true
            end

            at the end

            Frank

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 11 Aug 2011, 19:46

              I'll mostly use list.join("|") when the list is auto generated or loaded from a file.
              For instance user preferred fonts, in a cfg file. I'll never know what they might be, or how many the user would add to the file.
              So I'll build the droplist with .join('|')

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • D Offline
                Dan Rathbun
                last edited by 11 Aug 2011, 19:53

                @frankn said:

                Also in some examples that I found the don't use the UI in the inputbox command... again any advantage in doing either or?

                OK.. there is a wrapper method in "Tools/sketchup.rb" that defines the global lowercase inputbox()
                Read that file, and you'll see, Scott commented it well.

                The examples that use the uppercase global Inputbox class, use Jim Foltz' class wrapper.

                (Altho.. I think both really need to be some Lib or Mixin namespace, rather than in the global namespace. Hence the confusion, for the newbie.)

                I would suggest you do the wrapping in your own code the same way it's done in "sketchup.rb", rather than use that method in a global sense. Ie, teach yourself to control things, and wrap inside begin ... retry ... rescue .. end blocks, rather than rely on someone else's error handling.

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • T Offline
                  TIG Moderator
                  last edited by 11 Aug 2011, 19:59

                  The list = ["item1"|"item2"|"item3"] will fail as the syntax is wrong as the elements need ',' to separate them outside on the ""...
                  BUT it's eaiser to make a list of strings an the join them into one string with the [].join("|") method... 😒
                  inputbox() and UI.inputbox() are synomymous.

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    Dan Rathbun
                    last edited by 12 Aug 2011, 03:04

                    @tig said:

                    inputbox() and UI.inputbox() are synomymous.

                    Not exactly.

                    sketchup.rb:inputbox() wraps a call to UI.inputbox() within a begin .. rescue .. retry block, to trap TypeError exceptions, and re-display the inputbox, so the user can correct the input error(s).

                    It really would be better if this functionality were were just added to the native UI.inputbox() method.

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      TIG Moderator
                      last edited by 12 Aug 2011, 12:24

                      Dan, I stand corrected - clearly the skethcup.rb's 'inputbox' is to be preferred to the UI. version...

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      1 / 1
                      • First post
                        1/10
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement