PDA

View Full Version : ListBox indexing explained


Adam
11-01-2007, 02:58 PM
Question
How do I access data stored in a ListBox?

Answer
A ListBox is a great way to store data that a user can select at runtime to perform a function. For this example we will make a ListBox that stores files and then opens them when the user double clicks on one of the entries.

Create a ListBox object on the relevant page of your AutoPlay Media Studio project. For each entry in the ListBox have your data setup like this:

Item Text:
DocumentName

Item Data:
AutoPlay\\Docs\\mydocument.pdf

Now go to the Script tab and navigate to the 'On Double-Click' event.

The first action needed is ListBox.GetSelected(). This action returns a table containing all of the selected indexes of the ListBox. Since most document lists do not allow multiple selection we can make some assumptions about the returned data. Your action could look like this:

index = ListBox.GetSelected("ListBox1");

In this case we know that the selected index is stored in index[1]

The next step is to get the data that is associated with the selected index. The action ListBox.GetItemData(). This action will return the data associated with the selected item in the ListBox. We will need the selected index from the step above to get the data.

data = ListBox.GetItemData("ListBox1", index[1]);

Now the variable data contains the path to the selected document.

The next action is to open the document using a File.Open() action. The action will look like this

File.Open(data, "", SW_SHOWNORMAL);

The final script with comments will look like this

-- Get the selected index
index = ListBox.GetSelected("ListBox1");
-- Get the data associated with the selected item
data = ListBox.GetItemData("ListBox1", index[1]);
-- Open the document
File.Open(data, "", SW_SHOWNORMAL);