Populate a ListBox from a text file

For ListBox (or ComboBox) objects whose contents (items) remain unchanged, you just type the items into the List Items field at Design time. However, there are times when you require a different set of items to be loaded into such Boxes every time the VisualNEO Win created application is run … or the items are changed at run time and need to be saved/reloaded every time the program is stopped/restarted … this article suggests how you can implement such a feature within your application.

First, use something like notepad.exe to make up a file that looks like …

Australia
Brazil
Canada
Denmark
Egypt
France
Germany

… and save it in the same folder as your application … name it myCountryList.txt

Say the Listbox is called myCountries … in the List Items: field for this object type [myCountryList] … and in your Apps’s StartUp section … code something like this …

FileRead "[PubDir]myCountryList.txt" "All" "[myCountryList]"

That is all that is required … however, if you would like the ListBox to display the first item in the list (instead of a blank item), you can add a command after the FileRead command above.

If your ListBox is configured such that Variable contains: is set to Item number (1,2,3 …), then add this command …

SetVar "[SelectedCountry]" "1"

… assuming the ListBox was configured to have Variable: (to store selected item) set to [SelectedCountry].

If your ListBox is configured such that Variable contains: is set to Item text, then add this command …


ListBoxGetItem "myCountries" "1" "[SelectedCountry]"

… assuming the ListBox was configured to have Variable: (to store selected item) set to [SelectedCountry]