How To | Populate a Combo Box or List Box with values using Lua
Learn how to fill a Combo Box or List Box with values using the Lua programming language.
Procedure
To populate a Combo Box or List Box, simply set its .Choices to a table of values. For example, if you had a combo box called 'myComboBox', the following script would populate it with 3 strings ("value1", "value2", and "value3"):
Controls.myComboBox.Choices = {"value1", "value2", "value3"}
Another example is to use dir.get() to get all of the audio file names in a directory and then put them into a Combo Box or List Box. You could do this by creating a table of files and then setting the .Choices of the control to that table as seen below:
--Example to Add an item named Source to the Combo Box each time a button is pressed.
tblChoices = {} -- Start with no options for the Combobox
Controls.ComboBox.Choices = tblChoices -- Set the combobox to tblChoices
tick = 0 -- Count each time button is pressed
Controls.addAnotherOption.EventHandler = function() -- Adds "Source (TickNumber)" as a new option
if Controls.addAnotherOption.Boolean then
tick = tick + 1 --Increment tick counter
table.insert(tblChoices, "Source "..tick) -- Insert new Source tick into tblChoices
Controls.ComboBox.Choices = tblChoices -- Set ComboBox to tblChoices
end
end