ListBox.List (gb.qt4)
Returns or sets the
ListBox contents from a string array.
Example
This example shows how you can save and reload the content of a Listbox to a text file. When opening the file we use the
Split method so each line becomes on item in the Listbox. When saving the file we use the string[].Join method to create a single string with each list item on a line.
' Open a text file for display in the ListBox
PUBLIC SUB ButtonOpen_Click()
Dialog.Filter = ["*.lst", "Lists", "*.txt", "Text Files", "*", "All Files"]
IF Dialog.OpenFile() THEN RETURN
ListBoxItems.List = Split(File.Load(Dialog.Path), "\n")
CATCH
Message.Info("Cannot load list:\n" & Dialog.Path & "\n" & Error.Text)
END
' Save the ListBox content to a text file
PUBLIC SUB ButtonSave_Click()
Dialog.Filter = ["*.lst", "Lists", "*.txt", "Text Files", "*", "All Files"]
IF Dialog.SaveFile() THEN RETURN
File.Save(Dialog.Path, ListBoxItems.List.Join("\n"))
CATCH
Message.Info("Cannot save list:\n" & Dialog.Path & "\n" & Error.Text)
END
Note that that there is at least one defect with that example. If any item strings in the Listbox contain new lines than they will be split into two lines. Maybe not what you want.
To modify items using ListBox.List you must write the whole array and not to a single List[] item.
It is better to use the ListBox[] array directly (see
_get) to modify single items.
For example this will not work...
ListBox1.List[1] = "My text"
ListBox1.List.Remove(3)
Using the
ListBox1.List[] items like that calls the
List_Read() method that returns only a copy of the list so modifications will not take effect.
You may use something like the following...
Dim aList As String[] = ListBox1.List
aList[1] = "My text"
aList.Remove(3)
ListBox1.List = aList
Using
ListBox1.List = aList like that calls the
List_Write() method that WILL update the ListBox.
It is better though to simply use something like this...
ListBox1[1].Text = "My text"
ListBox1.Remove(3)