These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Combo Box populating Text Field with user override

Anpumes
Registered: Feb 3 2011
Posts: 7
Answered

Good morning all,
I'm new here, and new to JavaScript. I've been working on creating an interactive PDF document for several days now. I feel I have come a long way, simply reading the developer document using Google to find things I didn't understand, but I have reached an impasse.
 
I have a combo box that I am using to populate other text fields in the document, and thanks to nerdaggro and Thomp in another forum post, I got one of the fields to act exactly the way I wanted it to. I now have another field that I want to have set as readonly, so long as the user selects an input already available in the combo box, with values pulled for that selection. But if the user decides to type something different into the combo box, then I need the related field to become non-readonly and allow the user to imput his own data at that point.
 
What I currently have is what Thomp gave to nerdaggro in the previous post I mentioned. I've tried to modify it to add the removal of the readonly, to no avail.
 
I have a Text Field named "txtClass_Value". In the Custom Keystroke Script: Under the Format tab of the Text Field Properties window, I have:
 
if (!event.willCommit)
{
event.target.bDetect = true;
}
else if (event.value == "")
{
event. target.bDetect = false;
}
 
in the Custom calculation script: Under the Calculate tab of the Text Field Properties window, I have:
 
event.rc = !event.target.bDetect;
event.value = this.getField("cmbClass").value;
 
var A = this.getField("cmbClass").value;
 
if (A == "Select_1")
event.value = 12
else if (A == "Select_2" || A == "Select_3")
event.value = 10
else if (A == "Select_4" || A == "Select_5")
event.value = 8
else
event.value = ""
 
This Text Field is set to readonly using the check box found on the General tab. I've tried using:
 
this.getField("txtClass_Value").readOnly = false;
 
This has proved fruitless though, and no matter where I place this, I do not end up with what I am after. I'm really hoping that my question makes sense to someone out there, and that they are willing to assist me in this matter. I know I could just leave the fields set as not readonly, but then that adds a field that I may not want the user modifying, based on another selection. Maybe I'm trying to do this in the wrong location, or maybe I need it to be an action of some sort, I'm beyond my element now on this one.
 
Thanks again, in advance, for any help you may offer

My Product Information:
Acrobat Pro 9.4, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
In the Acrobat JavaScript Reference, the read only property for a field object is spelled with all lower case. Try this:

this.getField("txtClass_Value").readonly = false;

It's always to simple stuff;)

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Anpumes
Registered: Feb 3 2011
Posts: 7
Thank you for responding Thom. Your answer seems to make perfect sense to me, but I'm still having no success getting this to work. Since I'm new to Coding, I must be having some fundamental misunderstanding regarding the functions of things like:

!event.willCommit
event.target.bDetect

I am really lost now. I seem to think I understand what I'm doing, but apparently I do not.

I'm trying to get the "txtClass_Value" field to only be accessible to the user when the user has typed something into the "cmbClass" combobox. Other wise the user shouldn't be able to change the value in it. Am I using the code correctly by putting it the "txtClass_Value" properties? Does it belong somewhere else? I'm really beginning to think that I'm can't call on the field in which the code resides to get the readonly to work. I hate to be an annoyance, and I hope I'm not going beyond the scope of this forum with my questions/problems.

Thanks again.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Accepted Answer
Hmm, using the calculate event on a field that will also accept user input is tricky. Avoid doing this if possible.

You need to have a good understanding of the event sequence. To start off, put "console.println()" statements in all the events that are of interest. Then you can watch the events unfold as you enter data into the form fields.

Now, according to what you've said, the action is driven from the combobox, so I don't see any reason to use the calculate event on the text field. You can do everything from the the keystroke event.

The main issue is detecting the difference between a list selection and a real keystroke entry. The first thing you have to do is make sure the combox is setup correctly, that the options "Allow user to enter custom text" and "Commit selected value immediately" are both checked. Next, there are two main differences between a list selection and a real keystroke. 1) the event.changeEx value is blank for a real keystroke, 2) The event.selStart and event.selEnd properties are "-1" when the user selects from the list. Give this info the code can identify all conditions of interest.

Here's the Keystroke event code for identifying the conditions.

if (!event.willCommit)
{// called for individual keystrokes and selections
// Set bDetect to true for a real keystroke
event.target.bDetect = (event.selStart>=0);
}
else
{// Data is committed to the field

// Get Text Field of interest
var oFld = this.getField("txtClass_Value");

if(event.target.bDetect)
{// User Keystroke entry

if(event.value == "")
{// Empty entered value
.. code for handling conditon ...
}
else
{// User Entered a non-Empty value
oFld.readonly = false;
}
}
else
{// User made list selection
oFld.readonly = true;
.. code for setting text field value ...
}





// Clear Detection property
event. target.bDetect = false;
}





Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Anpumes
Registered: Feb 3 2011
Posts: 7
A great many thanks Thom,
That last bit of code did nearly exactly what I wanted it to. I now just have one odd little problem that I'm unsure how to correct. Previously when the code was all within the "txtClass_Value" field, a null value would null the field (No data showed at all), now a null value leaves the last properly selected value in the field.

Just to try to be clear. The code that used to reside in the "txtClass_Value" field is now in the "cmbClass" combobox. I reversed the way it works, to set a new variable that I then call back to the "txtClass_Value" field using that fields Calculate tab, custom calculation script (event.value = Class_Value). While that seems to work just fine as long as a number is returned, it doesn't work so well when the value is null (Class_Value = ""). Previously, any selection that wasn't on the combobox list would clear this fields value. Is there a way to still have this happen without messing up the current abilities of the box. Should I put (event.value = Class_Value) someplace else, or is the current location right. It seems like the best fit to me, but I don't know enough just yet. I am learning though.

Thanks again.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
All you need to do is to add a line of code to the script for clearing the text field. You could place this line in a specific location, like the area that detects keystroke entry. Or in a general location, such as the "WillCommit" portion of the code (immediately after the first "else").

There are 3 conditions outlined in the "WillCommit" portion of the script. The code should set ReadOnly and the field value for each condition.


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script