June 23, 2005
Followup: adding & removing items to/from components
In a follow-up to my previous post, I mentioned that I had two UI components (selection lists) that I wanted to be able to add to list2 from list1 by selecting items in list1 and clicking a button. Likewise, I wanted to remove selected items from list2 by clicking another button.
Because I’m pretty new with Actionscript 2.0, I’ve had to hunt-n-peck through the help guide in Flash and try different things out. So here’s all you need to do to make it work.
First you need two instances of a UI list box. For this example, List1 has a list of items to choose from. List2 is the target list where we want to transfer our selection to with the click of a button. Give the first UI list box an instance name of List1 and the second List2.
You’ll need two buttons, one to transfer selected items from List1 to List2 and one to remove selected items from List2.
On the transfer button, use this actionscript:
on (release) {
tempArr = new Array();
tempArr = _root.List1.getSelectedItems();
for (i=0; i
}
_root.List2.dataProvider = tempArr;
}
What this code essentially does is goes through List1 and throws the selected items into an array (tempArr). We cycle through that array and add the label and data to List2.
On the remove selection button, use this actionscript:
on (release) {
myArr = new Array();
myArr = List2.selectedItems;
for (i = 0;
i < myArr.length;
i++) {
List2.removeItemAt(List2.selectedIndex);
}
}
This snippet makes an array of the selected items in List2 and simply removes them from the List2 UI list. Pretty straight forward stuff.
The most complicated part of getting to this was understanding what I could and couldn’t do with these lists. Hope you find this useful.

June 23, 2005, 9:46 am
Filed under: Actionscript, Flash (Macromedia)
Leave a Comment
You must be logged in to post a comment.

