Using List Boxes
For both List boxes and Combo boxes, the control index
determines how many items are in the box. Once this value is set, you set the text of List box items by specifying a character string for each item index. Indexes run from 1 to the total number of list items set with
. For example:
retlog = DlgSet ( dlg, IDC_LISTBOX1, 3, DLG_NUMITEMS )
retlog = DlgSet ( dlg, IDC_LISTBOX1, "Moe", 1 )
retlog = DlgSet ( dlg, IDC_LISTBOX1, "Larry", 2 )
retlog = DlgSet ( dlg, IDC_LISTBOX1, "Curly", 3 )
These function calls to
put three items in the List box. The initial value of each List box entry is a blank string and the value becomes nonblank after it has been set.
You can change the list length and item values at any time, including from within callback routines. If the list is shortened, the set of entries is truncated. If the list is lengthened, blank entries are added. In the preceding example, you could extend the list length and define the new item with the following:
retlog = DLGSET ( dlg, IDC_LISTBOX1, 4)
retlog = DLGSET ( dlg, IDC_LISTBOX1, "Shemp", 4)
Since List boxes allow selection of multiple entries, you need a way to determine which entries are selected. When the user selects a List box item, it is assigned an integer index. You can test which list items are selected by reading the selection indexes in order until a zero value is read. For example, if in the previous List box the user selected Moe and Curly, the List box selection indexes would have the following values:
If Larry alone had been selected, the List box selection index values would be:
To determine the items selected, the List box values can be read with
until a zero is encountered. For example:
INTEGER j, num, test
INTEGER, ALLOCATABLE :: values(:)
retlog = DLGGET (dlg, IDC_LISTBOX1, num, DLG_NUMITEMS)
j = 1
test = -1
DO WHILE (test .NE. 0)
retlog = DLGGET (dlg, IDC_LISTBOX1, values(j), j)
test = values(j)
j = j + 1
In this example, j is the selection index and values(j) holds the list numbers, of the items selected by the user, if any.
To read a single selection, or the first selected item in a set, you can use
, since for a List Box
holds the character string of the first selected item (if any). For example:
! Get the string for the first selected item.
retlog = DLGGET (dlg, IDC_LISTBOX1, str, DLG_STATE)
Alternatively, you can first retrieve the list number of the selected item, and then get the string associated with that item:
! Get the list number of the first selected item.
retlog = DLGGET (dlg, IDC_LISTBOX1, value, 1)
! Get the string for that item.
retlog = DLGGET (dlg, IDC_LISTBOX1, str, value)
In these examples, if no selection has been made by the user, str will be a blank string.
Window in the Resource Editor, List boxes can be specified as sorted or unsorted. The default is sorted, which causes List box items to be sorted alphabetically starting with A. If a List box is specified as sorted, the items in the list are sorted whenever they are updated on the screen. This occurs when the dialog box is first displayed and when the items are changed in a callback.
The alphabetical sorting follows the ASCII collating sequence, and uppercase letters come before lowercase letters. For example, if the List box in the example above with the list "Moe," "Larry," "Curly," and "Shemp" were sorted, before a callback or after
returned, index 1 would refer to "Curly," index 2 to "Larry," index 3 to "Moe," and index 4 to "Shemp." For this reason, when using sorted List boxes, indexes should not be counted on to be the same once the dialog is displayed and any change is made to the list items.
You can also call
with the DLG_ADDSTRING index to add items to a List box or Combo box. For example:
retlog = DlgSet(dlgtab, IDC_LIST, "Item 1", DLG_ADDSTRING)
When you use DLG_ADDSTRING, the DLG_NUMITEMS control index of the List or Combo box is automatically incremented.
When adding items to a sorted list or Combo box, using DLG_ADDSTRING can be much easier than the alternative (setting DLG_NUMITEMS and then setting items using an index value), because you need not worry about the list being sorted and the index values changing between calls.