Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dialog box script: if condition on chosen combo box value?

    This is my first time creating a dialog box, and I am struggling to create conditions based on chosen values from a COMBOBOX dropdownlist.

    A simplification of what I want is in the code below.

    The user selects your favorite fruit from the options apple, banana, other. Then, I want to enable/disable an EDIT field according to whether other was chosen in the drop down list, so the user may type his custom answer.

    Any help would be much appreciated!


    Code:
    VERSION 15.0
    
    POSITION . . 200 60
    
    DIALOG main, label("Help") tabtitle("Main")
    BEGIN
    
      TEXT     tx_fruit     10   10  120  .,    label("Your favorite fruit is...")
      COMBOBOX cb_fruit      @  +20  60   .,                            ///
               dropdownlist                                             ///
               contents(fruit_list)                                     ///
               onselchange(script main_cb_fruit_change)
      EDIT     ed_fruit     +60  @  100    .,                           ///
               tooltip("For example: pear")
    
    END
    
    LIST fruit_list
    BEGIN
      apple
      banana
      other
    END
    
    SCRIPT main_cb_fruit_change
    BEGIN
      ***** HELP NEEDED: HOW TO CORRECT THE LINE BELOW? ****
      if main.cb_fruit == "other" {
        main.ed_fruit.enable
      }
      else {
        main.ed_fruit.disable
      }
    END
    
    OK ok1,      label("OK")
    CANCEL can1, label("Cancel")
    RESET res1
    
    
    PROGRAM command
    BEGIN
        put `"display "Thanks for the help""'
    END

  • #2
    I modified your code a bit. The solution below might not be the most effective way, but it works. I added a new list which contains scripts for every fruit. The scripts following that list do exactly what your original code does.
    The problem with your code is that a SCRIPT cannot have if-statements or any other control structure. Only a PROGRAM can.
    Code:
    VERSION 14.2 // I am only on Stata 14.2
    POSITION . . 200 60
    LIST fruit_list
    BEGIN
      apple
      banana
      other
    END
    
    // I added the following list and scripts
    LIST fruit_options
    BEGIN
     script apple_options
     script banana_options
     script other_options
    END
    
    SCRIPT apple_options
    BEGIN
        main.ed_fruit.disable
    END
    
    SCRIPT banana_options
    BEGIN
        main.ed_fruit.disable
    END
    
    SCRIPT other_options
    BEGIN
        main.ed_fruit.enable
    END
    
    DIALOG main, label("Help") tabtitle("Main")
    BEGIN
    
      TEXT     tx_fruit     10   10  120  .,    label("Your favorite fruit is...")
      COMBOBOX cb_fruit      @  +20  60   .,                            ///
               dropdownlist                                             ///
               contents(fruit_list)                                     /// Changed onselchange(script main_cb_fruit_change) to onselchangelist()
               onselchangelist(fruit_options)
      EDIT     ed_fruit     +60  @  100    .,                           ///
               tooltip("For example: pear")
    
    END
    
    
    /* The old code
    SCRIPT main_cb_fruit_change
    BEGIN
      ***** HELP NEEDED: HOW TO CORRECT THE LINE BELOW? ****
      if main.cb_fruit == "other" {
        main.ed_fruit.enable
      }
      else {
        main.ed_fruit.disable
      }
    END
    */
    
    OK ok1,      label("OK")
    CANCEL can1, label("Cancel")
    RESET res1
    
    
    PROGRAM command
    BEGIN
        put `"display "Thanks for the help""'
    END

    Comment


    • #3
      Awesome! Works perfectly.
      Thank you.

      Comment

      Working...
      X