16 Replies Latest reply on May 28, 2014 11:56 AM by George_Johnson

    Multi Selection Listbox-Textbox

    DanMun Level 1

      I am building a form that will contain several list boxes with statements (text) in them.  I would like the end used to be able to highlight the selections in the listbox and then transfer those selections to a listbox or textbox.  I will then populate a template with the final selections from the selctions entered into the final textbox.  I have received a code from maxwyss that allows me to place one selection from the listbox into a textbox using a command button.  The script for the listbox is:

       

      var cRtn =
      this.getField("text1").value = cRtn;

       

      Placed in the "Selection Change" "execute this script" window.

       

      The script for the button is:

       

      this.getField("text1").value = this.getField("Problems").valueAsString;

       

      Placed in the "Actions" tab javascript editor console of the button.

       

      Where text1 is the textbox and "Problems" is the listbox.

       

      This works great (thank you) but I need to place multiple selections from the listbox into the textbox.  I hope I am explaining myself correctly.

       

      I would really appreciate any help out there.  If I accomplish this then the project will be perfect.

       

      Thank you so much

      Dan M.

        • 1. Re: Multi Selection Listbox-Textbox
          DanMun Level 1

          I also have a second set up where the list box code is: Placed in the

           

          if(xfa.event.prevText=="")
          {
          SelectedCat.rawValue = xfa.event.newText;
          }
          if(xfa.event.prevText!="")
          {
          SelectedCat.rawValue = SelectedCat.rawValue+"\n"+xfa.event.newText;
          }

           

          Placed in the "Selection Change" "execute this script" window

           

          This allows me to place long sentences in the target textbox using a buttom with the code:

           

          this.getField("text8").value = this.getField("Problems1").valueAsString ;

           

          But I still can't enter multiple sentences in the textbox.

           

          Any ideas are very welcome.

           

          Dan M.

          • 2. Re: Multi Selection Listbox-Textbox
            George_Johnson MVP & Adobe Community Professional

            That second bit of code is for an XFA form created in LiveCycle Designer. Are you using Acrobt of LiveCycle Designer to create your form?

             

            To enter more than one selected item in the text box, you'd have to first get the value of the text box, get the value of the list box, and concatenate, perhaps with some sort of separator such are a carriage return. Something like:

             

            (function () {
            
                // Get references to the text field and list box
                var f1 = getField("text8");
                var f2 = getField("Problems1");
            
                // Get the field values
                var v1 = f1.valueAsString;
                var v2 = f2.valueAsString;
            
                // Define the separator here
                var sSeparator = "\r";
            
                // Concatenate the list box value to the text field value, including
                // the separator if needed.
                if (v1) {
                    f1.value = v1 + sSeparator + v2;
                } else {
                    f1.value = v2;
                }
            
            })();
            

             

            The first and last lines prevent the unnecessary creation of document-global variables, which is good.

            • 3. Re: Multi Selection Listbox-Textbox
              DanMun Level 1

              Thank you for your efforts.  I am using acrobat 9.0 not livecycle.

               

              I tried to use your code but must not be putting it in the right place. I placed it in the javascript for the button, but it doesn't seem to accomplish the task.  I am not the most savy with scripting.

               

              Thank you so much George for your help!

               

              Almost there.

              • 4. Re: Multi Selection Listbox-Textbox
                George_Johnson MVP & Adobe Community Professional

                Yes, it was intended to be the Mouse Up script for a button. Do you get any errors in the JavaScript console (Ctrl+J)?

                • 5. Re: Multi Selection Listbox-Textbox
                  George_Johnson MVP & Adobe Community Professional

                  BTW, the code that uses xfa, rawValue, etc. is for an XFA form, so it should not work and should be giving you errors.

                  • 6. Re: Multi Selection Listbox-Textbox
                    DanMun Level 1

                    Yes, multiple errors related to xfa no being identified.  I did not notice

                    the errors as nothing "popped up" so to speak, but they are definitely

                    there.

                     

                    On second try your script works very well.  It does not enter multi selected

                    items all at once, but will enter the items 1 by one into the same text box

                    which is very very helpful!

                     

                    Thank you so much!!!

                    Dan M.

                    • 7. Re: Multi Selection Listbox-Textbox
                      George_Johnson MVP & Adobe Community Professional

                      I missed the part about needing to get multiple items if they are selected. In that case, you have to get the value of the list box using the value property, not the valueAsString property. It will be an array if multiple items are selected. You would then have to get each array item and add concatenate it, separating each with the separator (\r). It would be best to set up a loop to do this. If you get stuck, post again and someone can help.

                       

                      BTW, you should set up your JavaScript preferences so that the console is shown when an error occurs: Edit > Preferences > JavaScript > Show console on errors and messages

                      • 8. Re: Multi Selection Listbox-Textbox
                        DanMun Level 1

                        I will set that up-I must have de-selected it at some point.  Thanks again

                        for your help.  I think the way it is now is actually better than my

                        original plan.

                        • 9. Re: Multi Selection Listbox-Textbox
                          DanMun Level 1

                          George,

                           

                          Can you think of any reason why this script would not work with more than one wet of listboxes and textboxes?  I can't seem to get it to work more than once in the same form.  I am obviously changing the names of the fields, but it doesn't want to work after the first set.

                           

                          Thanks.

                          • 10. Re: Multi Selection Listbox-Textbox
                            jck_drmjn Level 1

                            George_Johnson wrote:

                             

                            That second bit of code is for an XFA form created in LiveCycle Designer. Are you using Acrobt of LiveCycle Designer to create your form?

                             

                            To enter more than one selected item in the text box, you'd have to first get the value of the text box, get the value of the list box, and concatenate, perhaps with some sort of separator such are a carriage return. Something like:

                             

                            (function () {
                            
                                // Get references to the text field and list box
                                var f1 = getField("text8");
                                var f2 = getField("Problems1");
                            
                                // Get the field values
                                var v1 = f1.valueAsString;
                                var v2 = f2.valueAsString;
                            
                                // Define the separator here
                                var sSeparator = "\r";
                            
                                // Concatenate the list box value to the text field value, including
                                // the separator if needed.
                                if (v1) {
                                    f1.value = v1 + sSeparator + v2;
                                } else {
                                    f1.value = v2;
                                }
                            
                            })();
                            

                             

                            The first and last lines prevent the unnecessary creation of document-global variables, which is good.

                            Hi George,

                             

                            Nice. It works well.

                            • 11. Re: Multi Selection Listbox-Textbox
                              George_Johnson MVP & Adobe Community Professional

                              It should work any number of times. Do you see any errors in the JavaScript console (Ctrl+J)?

                              • 12. Re: Multi Selection Listbox-Textbox
                                DanMun Level 1

                                No errors in the console.  Here is what I have in the second button for the listbox and textbox.  Can't seem to get it work.  The first one works great.

                                Where probselected2 is the textbox and problems2 is the list box set to multi select like the first example.  I must be doing something wrong?

                                 

                                 

                                (function () {

                                 

                                     // Get references to the text field and list box

                                    var f1 = getField("probselected2");

                                    var f2 = getField("problems2");

                                 

                                    // Get the field values

                                    var v1 = f1.valueAsString;

                                    var v2 = f2.valueAsString;

                                 

                                    // Define the separator here

                                    var sSeparator = "\r";

                                 

                                    // Concatenate the list box value to the text field value, including

                                   // the separator if needed.

                                     if (v1) {

                                        f1.value = v1 + sSeparator + v2;

                                     } else {

                                         f1.value = v2;

                                     }

                                 

                                })();

                                • 13. Re: Multi Selection Listbox-Textbox
                                  DanMun Level 1

                                  I figured it out. My listbox was not registering the selection.  I had to highlight and change form sort to multi select a few times and then the list selected items would highlight and the script/button works great.  Thanks for all your help and patients with a definite novice.

                                   

                                  Dan M.

                                  • 14. Re: Multi Selection Listbox-Textbox
                                    arvindkumar7 Level 1

                                    HI ,

                                     

                                    The following code is populating the text field with selected item name from the list box whether a single or multiple. But I want to have bound values to those item name in the text field instead. Please help. This code is working fine but only with item name returning to text field.

                                    Thanks

                                     

                                    TextField.rawValue = "";
                                    //loop through items in listbox
                                    for(i = 0; i < ListBox.length; i++)
                                    {
                                             //if item selected
                                              if(ListBox.getItemState(i))
                                              {
                                                        //check if Text field value if null
                                                        if (TextField.isNull)
                                                        {
                                                                  TextField.rawValue = ListBox.getDisplayItem(i);
                                                        }
                                                        else
                                                        {
                                                                  TextField.rawValue = TextField.rawValue + " " + ListBox.getDisplayItem(i);
                                                        }

                                     

                                     

                                     

                                              }

                                     

                                    }

                                    • 15. Re: Multi Selection Listbox-Textbox
                                      try67 MVP & Adobe Community Professional

                                      Try the LiveCycle Designer forum.

                                      • 16. Re: Multi Selection Listbox-Textbox
                                        George_Johnson MVP & Adobe Community Professional

                                        Since you're working with a form created in LiveCycle Designer, you should post your question in the LiveCycle Designer forum. This forum is for questions about JavaScript created in Acrobat.