A list box form field is very similar to a dropdown (“combo box”) field. With the list box you can see multiple items. If there are more items than the space available you can scroll down the list. Dropdowns only show one item until the field is expanded.
We created a tool to fill list boxes and dropdowns with multiple items by simply copying and pasting a column from Excel.
Multiple Item Selection
One of the differences between dropdowns and list boxes is that list boxes have an option to select multiple items. This is done by holding down the Ctrl key on the keyboard (Windows OS) and selecting items with the mouse. To select multiple consecutive items, the first item can be selected and then last item while holding down the Shift key.
This is not intuitive to a lot of users, so instructions should be provided in the form if this option is selected in the field properties.
If only one item is selected, the value of the field is returned as a string. If more than one item is selected, the value returns an array (a comma separated list). Multiple selection and Commit selected value immediately (from the Options tab of the field properties) are mutually exclusive. In other words, they can’t both be checked at the same time. If one is checked, it disables the other.
I Love A Challenge
A participant in the Acrobat User Forum had a question about how to limit the number of List Box selections to three. Since Multiple selection had to be checked to allow multiple items to be selected, Commit selected value immediately could not be checked. My first thought was a custom keystroke script to limit the number of selections. When Commit selected value immediately is not checked, the selections do not commit until a blur event (tabbing out of the field or clicking anywhere else on the screen). This proved to be quite challening since a keystroke script could not keep track of selections without exiting the field. I would have to think outside the box and try some other methods.
Challenge Accepted
I created a text field called Decoy and then used a Mouse Up action in the list box field to set the focus to the decoy field. Every time a selection was made (“Mouse Up”), the focus moved to the Decoy field, thereby committing the list box values. The Decoy field had one On Focus action script:
//1. Define a variable for the list box field
var box=this.getField("List Box");
//2. Test the type of value
if(typeof box.value=="object")
{
//3. Test if the length is equal to 3
if(box.value.length==3)
{
//4. Define a variable for the list box value
var vals=box.value;
}
//5. Test if the list box length is greater than 3
else if (box.value.length>3)
{
//6. Set the list box value back to the previous value when there were 3 selections
box.value=vals;
//7. Warn the user of the limit
app.alert("The maximum number of selections is 3.",1);
}
}
//8. Set the focus back to the list box field
box.setFocus();
Here’s the script without the comments:
var box=this.getField("List Box");
if(typeof box.value=="object")
{
if(box.value.length==3)
{
var vals=box.value;
}
else if (box.value.length>3)
{
box.value=vals;
app.alert("The maximum number of selections is 3.",1);
}
}
box.setFocus();
Script Explained
(From the numbered code block above)
Define a variable for the list box field object, since it will be used many more times in the script.
Test the type of list box value. If the type is not an object, then it is a string, which means that only item is selected. Therefore none of the remaining script needs to run except the last line (setting the focus back to the list box).
Test whether the length of the object (array) is equal to 3.
If the length is equal to 3, set a variable for the value of the list box. This will be used to maintain the selected items to this point if the user selects a fourth item.
If the list box value is an object, and the length of it does not equal 3, then it must be greater 3.
Set the list box value back to the variable created when 3 items were selected.
Pop an alert box that warns the user that the limit is 3.
Set the focus back to the list box. This line of code is not contained within any of the conditional statements so it always runs, returning the focus back to the list box.
NOTE: To change the limit from 3, change all of the number 3’s in the script to the desired limit.
One More Thing
The script in the Decoy field won’t run if the field is hidden or set to read only. You don’t want anyone interacting with it or seeing it, so what’s the solution? Set all sides of its rectangle to zero so it is an invisible speck in the bottom left corner of the page by running the following script in the console:
this.getField("Decoy").rect=[0,0,0,0];
You might want to make it the last tab in the tabbing order as well. How? Manually setting the tabbing order is included here:
How to List All Field Names in Tab Order in Adobe Acrobat
“I need to extract the names of all form fields in their tab order. Is there a way to do this using JavaScript or any built-in functionality?”