Moving Selected Item in UI List

So following up to my other follow-ups ;) on the UI list selection boxes, I’ve added functionality that can move a selection up or down in the list order. In this example, the UI list box instance name is selectedModulesBox.

Here’s the code that makes it move up in the sort order:

// ######## this example moves an item up in the sort order (example: from index 2 to index 1)
on (release) {
// the UI list instance name is "selectedModulesBox"
selectedIndexNumber = selectedModulesBox.selectedIndex; // get current selection index number
previousIndexNumber = selectedIndexNumber - 1; // get the destination index number
if (selectedIndexNumber > 0) {
// temporarily store the data we're going to be moving into some variables so we don't lose it
movingSelectedItem = selectedModulesBox.getItemAt(selectedIndexNumber);
movingPriorItem = selectedModulesBox.getItemAt(previousIndexNumber);
// replace the data in originating and destination indices
selectedModulesBox.replaceItemAt(previousIndexNumber, movingSelectedItem.label, movingSelectedItem.data);
selectedModulesBox.replaceItemAt(selectedIndexNumber, movingPriorItem.label, movingPriorItem.data);
// move the selection box up in the order
selectedModulesBox.selectedIndex = previousIndexNumber;
}
}

to move it down in the sort order, use this code:

on (release) {
selectedIndexNumber = selectedModulesBox.selectedIndex;
nextIndexNumber = selectedIndexNumber + 1;
if (nextIndexNumber < selectedModulesBox.length) {
movingSelectedItem = selectedModulesBox.getItemAt(selectedIndexNumber);
movingNextItem = selectedModulesBox.getItemAt(nextIndexNumber);
selectedModulesBox.replaceItemAt(nextIndexNumber, movingSelectedItem.label, movingSelectedItem.data);
selectedModulesBox.replaceItemAt(selectedIndexNumber, movingNextItem.label, movingNextItem.data);
selectedModulesBox.selectedIndex = nextIndexNumber;
}
}

What is actually forcing the selection highlight to move with the change in order is the line: selectedModulesBox.selectedIndex = nextIndexNumber;. What that’s doing is simply saying that the new selectedIndex value in the UI list is the destination index number.

Lose ya? Leave your questions and I might be able to answer them.

Digg This
June 23, 2005, 12:01 pm

Leave a Comment

You must be logged in to post a comment.