Recipe 14.5. Preselecting Menu Control Values


Problem

You want a Flash form to initialize with values already selected in a combo box or list.

Solution

For a combo box or single-select list, assign a value to the selectedIndex property. For a multiselect list, assign a value to the selectedIndices property.

Discussion

You can use the selectedIndex property to programmatically select a value from a combo box or list component instance. In order for that approach to work, of course, you'll need to know the index of the item you want to select. Each item in the combo box or list has a whole number index. The first item has an index of 0, the second has an index of 1, and so on. Therefore, if you know you want to programmatically select the third item in the combo box, you need only assign the value of 2 to the selectedIndex property. For example, if you have a combo box named ccbProducts, you can programmatically select the third item with the following code:

 ccbProducts.selectedIndex = 2; 

However, more often than not, you will know the value of the item you want to select rather than the index. If that occurs, you will need to use a for statement to loop through all the items of the combo box, checking each to see whether it matches the value you want to select. You can use the getItemAt() method to retrieve a combo box item of a particular index. You can use the length property to retrieve the number of items in the combo box. So with that in mind, the following code shows an example in which the code loops through the items of a combo box named ccbProducts. When it finds an item with the label Flash, it sets the selectedIndex to that item's index.

 // Loop from 0 until the length of the ccbProducts indices is reached. for(var i:Number = 0; i < ccbProducts.length; i++) {   // Check to see if the current item's label matches the value of Flash.    if(ccbProducts.getItemAt(i).label == "Flash") {     // If so, set the combobox's selected index to the item's index.     ccbProducts.selectedIndex = i;   } } 

Although the preceding code will work, it can be made more efficient by adding one extra line of code. As it stands, Flash will continue to loop through the remaining items in the combo box even after the matching item is found. However, that is a waste of processing time. When the match has been found, you can tell Flash to stop the loop by using a break statement. The following code is identical to the preceding example, except that it adds the break statement to make it more efficient:

 for(var i:Number = 0; i < ccbProducts.length; i++) {  if(ccbProducts.getItemAt(i).label == "Flash") {    ccbProducts.selectedIndex = i;    break;  } } 

If you are using a single-select list, you can use the same basic code as when using a combo box. However, if you are using a multiselect list, you will need to use the selectedIndices property instead of the selectedIndex property. The selectedIndices property, as the name would suggest, allows you to specify multiple indices that should be selected. The property value should be in the for statement of an ActionScript array in which each of the elements is an index within the list that should be selected. For example, the following code assigns an array with four indices to the selectedIndices property of a list named clFavorites. When assigned, the list will display with the four corresponding elements selected.

 clFavorites.selectedIndices = [2, 4, 7, 13]; 

For the selectedIndices property to work properly you must set the multipleSelection property to true as well.


As with combo boxes and single-selected lists, when you are preselecting values for multiselect lists, you are likely to know the value rather than the index of each item you want to select. Therefore you need to use a for statement to add index values to an array before assigning that array to the selectedIndices property of the list. The following code uses a for statement to loop through all the items of the list. For each item in which the label is either Flash Cookbook or ActionScript Cookbook, the corresponding index is added to an array using the array's built-in push( ) method. Then, when the array has been populated, it is assigned to the list's selectedIndices property.

 // Create a new array into which the indices should be stored. var aIndices:Array = new Array(); // Loop through all the elements of the list. for(var i:Number = 0; i < clFavorites.length; i++) {   // Check to see if the label of the list item is either Flash Cookbook or ActionScript Cookbook.   if(clFavorites.getItemAt(i).label == "Flash Cookbook" ||       clFavorites.getItemAt(i).label == "ActionScript Cookbook")   {     // If the label is a match with either of the two titles then use the push( ) method to add     // the corresponding index to the aIndices array.     aIndices.push(i);   } } // Assign the aIndices array to the selectedIndices property. clFavorites.selectedIndices = aIndices; 

See Also

Recipe 13.3, Recipe 13.12, Recipe 13.13




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net