For this example, we'll calculate the total number of months in a specified number of years.
Prerequisites
- You should be familar with Lectora
- You should have a basic understanding of actions and action groups
1. Create a page in Lectora
First, create a blank project in Lectora or create a new page in an existing project.
2. Add the Required Objects
Add a text box. The text box can sometimes be hard to find. Go to Add >> Object >> Form Object >> Entry Field. Name the textbox "Years". Name the associated variable "Years" also.
Add a button. Label the button "Calculate Months".
Add a label. This is where we will but the results. Name it "Months".
You should now have something that looks similar to the following:

3. Create the Action Group
Create an action group. Name it "Calculate Months". Your tree view in Lectora should now look something like the following:
_cropped.png)
You'll be adding two actions to the action group. The first action will calculate the months. The second action will update the page with the new value. You will need to make sure these are ordered as stated to display the correct results.
Add a Modify Variable Action. This is where you will do the calculation of years to months by multipling by 12. Notice the calcuation will change the value of the variable "Years" to months. Set the following:
- Action Name: Multiply by 12
- Action: Modify Variable
- Target: Years
- Value: 12
- Modification Type: Multiply Variable By

Add a Change Contents Action. This will display the resultant number of months (now in the variable "Years") to the learner. Set the following:
- Action Name: Update Months
- Action: Change Contents
- Target: Months
- New Content: Years

4. Link the Action Group to the Button
Now you will need to go back to the button and edit it so that when the learner clicks it, the calculation will be made. Go into the button properties and choose the "On Click" tab. Set the following:
- Action: Run Action Group
- Target: Calculate Months
5. Preview the Project
All that is left to do is to see the code in action. Preview the project and notice any number you put in will be calculated into months when you click the button.

What happens if you don't put in a number? The calculation will not work and the text will show up in the results.

You may want to check the value in the years textbox to make sure it only contains numbers. Otherwise, you'll want to let your learner know it is not a valid number. For this you will need JavaScript. Look for a follow-up posting on that topic!
Great stuff- but how can I set the color and font size of the resulting display of calculqated number of months (in your example, it is red- I would like it red as well, but also larger.)
ReplyDeleteRandy Mason
randy.mason@boehringer-ingelheim.com