Keep Track of List Selections - Greg Collins
in

InfoPath Dev

This Blog

Syndication

Greg Collins

Keep Track of List Selections

At some point it might be necessary to keep track of order of the selections a user makes in a single list box or drop-down list box. You can accomplish this by writing your own code in an OnAfterChange event handler, or you can use rules.

In this task we will create a list box and track the order of the user's selections in another field in the main data source. We will examine both approaches mentioned in the opening paragraph. Let's start by designing a new blank form.

Create the schema:

  1. Open the Data Source task pane.
  2. Add a Text Field named SelectionsViaRules.
  3. Add a Text Field named SelectionsViaCode.
  4. Add a Text Field named Choice.

Design the view:

  1. Right-click myFields, and then choose Controls.
  2. Add a carriage return before each text box to place them on a new line.
  3. Double-click the text box labeled Selections Via Rules.
  4. On the Display tab of the Text Box Properties dialog box, select Read-Only check box and the Wrap Text check box, and then choose Expand To Show All Text from the Scrolling drop-down list.
  5. On the Size tab, set the width value to 651 (as shown in Figure 1), and then click OK.


Figure 1. Adjusting the text box properties.

  1. Make all of the same adjustments to the text box labeled Selections Via Code as you did to the previous text box.
  2. Right-click the text box labeled Choice, and then choose Change To | List Box.

Populate the list box:

  1. Double-click the List Box.
  2. On the Data tab of the List Box Properties dialog box, add five entries with values for the letters A, B, C, D, and E (as shown in Figure 2), and then click OK.


Figure 2. Adding the choices to the list box.

  1. Stretch the list box down to reveal all of the entries we just added.


APPROACH 1 – USE RULES TO TRACK SELECTIONS

The most convenient approach for the design we have chosen for our form is to use rules to keep track of selections in the list box. The rules will allow us to filter out selections that we don't want to keep track of, such as the default blank selection, and to determine whether a previous selection has already been made so we know whether to add a separator between the selections.

Add a rule for the first selection:

  1. Double-click the list box.
  2. Click Rules, click Add, and then name the rule Set Selection.
  3. Click Set Condition.
  4. For the first condition, choose Choice, and Is Not Blank from the drop-down lists.
  5. Click And, and then ensure that And, and not Or, is selected in the drop-down list.
  6. For the second condition, choose SelectionsViaRules, and Is Blank from the drop-down lists (as shown in Figure 3), and then click OK.


Figure 3. The conditions of the Set Selection rule.

  1. Click Add Action.
  2. Select Set A Field's Value in the Action drop-down list.
  3. Click the Select XPath button to the right of the Field text box, select SelectionsViaRules, and then click OK.
  4. Click the Insert Formula button to the right of the Value text box, type the dot character, and then click OK twice.

    The dot character is the XPath abbreviated syntax for the self::node() location. In this scenario it represents the Choice node.

  5. Select the Stop Processing Rules When This Rule Finishes check box, and then click OK.

Add a rule for additional selections:

  1. Click Add, and then name the rule Add Selection.
  2. Click Set Condition.
  3. For the first condition, choose Choice, and Is Not Blank from the drop-down lists, and then click OK.
  4. Click Add Action.
  5. Select Set A Field's Value in the Action drop-down list.
  6. Click the Select XPath button to the right of the Field text box, select SelectionsViaRules, and then click OK.
  7. Click the Insert Formula button to the right of the Value text box, and then type the following formula (as shown in Figure 4):

concat(../my:SelectionsViaRules, ", ", .)

Figure 4. The formula to add an additional selection to the text box.

  1. Click OK five times.

As this formula might look confusing, what it is doing is concatenating the existing value of the SelectionsViaRules text box with a comma and a space character, and then with the currently selected value of the Choice list box.


APPROACH 2 – USE CODE TO TRACK SELECTIONS

If we have chosen a form design that is not conducive to using rules or we need more complex processing capabilities, then we should use code to keep track of selections in the list box. In this task we will only duplicate in code the behavior of the rules from the previous approach.

Add code behind selection changes:

  1. Double-click the list box.
  2. On the Data tab of the List Box Properties dialog box, click Data Validation.
  3. In the Data Validation dialog box, select OnAfterChange from the Events drop-down list, and then click Edit.
  4. In the Microsoft Script Editor, replace the contents of the OnAfterChange event handler with the following code:

if(eventObj.IsUndoRedo || "Insert" != eventObj.Operation)
    return;

var oChoice = XDocument.DOM.selectSingleNode("my:myFields/my:Choice");
if("" == oChoice.text)
    return;

var oSelectionsViaCode = XDocument.DOM.selectSingleNode("/my:myFields/my:SelectionsViaCode");
if("" == oSelectionsViaCode.text)
    oSelectionsViaCode.text = oChoice.text;
else
    oSelectionsViaCode.text += ", " + oChoice.text;

  1. Choose Save from the File menu, and then close the Microsoft Script Editor.

Try it:

With the rules and code in place, let's preview the form. Make the following selections in the list box: A, B, D, A, C, and then E. As shown in Figure 5, both the text boxes keep track of the selections you made in the order you made them. They also include a separator between each selection.


Figure 5. The order of the list box selections is stored in text fields.

The text box used to store the user selections does not need to be visible in the view. If you do want to present them to the user, you can use an expression box instead of a read-only text box. You can also use a secondary data source instead of the main data source to keep track of the user selections if you do not have a need to save them as part of the form.

©2004 Greg Collins. All rights reserved. Licensed to Autonomy Systems, LLC for display on InfoPathDev.com.

Published Jun 09 2005, 06:56 AM by Greg Collins
Filed under: , ,

Comments

No Comments
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.