Creating Graphical User Interfaces |
List boxes display a list of items and enable users to select one or more items.
The String
property contains the list of strings displayed in the list box. The first item in the list has an index of 1. Using the Property Inspector, enter the list box items, one per line.
The Value
property contains the index into the list of strings that corresponds to the selected item. If the user selects multiple items, then Value
is a vector of indices.
By default, the first item in the list is highlighted when the list box is first displayed. If you do not want any item highlighted, then set the Value
property to empty, []
. This works only when multiple selection is enabled.
The ListboxTop
property defines which string in the list displays as the top most item when the list box is not large enough to display all list entries. ListboxTop
is an index into the array of strings defined by the String
property and must have a value between 1 and the number of strings. Noninteger values are fixed to the next lowest integer.
Single or Multiple Selection
The values of the Min
and Max
properties determine whether users can select single or multiple items in the list box:
Max - Min > 1
, the user can select multiple items.
Max - Min <= 1
, the user can only select a single item.
See Single or Multiple Line Selection.
Selection Type
List boxes differentiate between single and double clicks on an item and set the figure SelectionType
property to normal
or open
accordingly. See Triggering Callback Execution for information on how to program multiple selection.
Triggering Callback Execution
MATLAB evaluates the list box's callback after the mouse button is released or a keypress event (including arrow keys) that changes the Value
property (i.e., any time the user clicks on an item, but not when clicking on the list box scrollbar). This means the callback is executed after the first click of a double-click on a single item or when the user is making multiple selections.
In these situations, you need to add another component, such as a Done button (push button) and program its callback routine to query the list box Value
property (and possibly the figure SelectionType
property) instead of creating a callback for the list box. If you are using the automatically generated M-file option, you need to either:
Callback
property to the empty string (''
) and remove the callback subfunction from the GUI M-file.
The first choice is best if you are sure you will not use the list box callback and you want to minimize the size and efficiency of the GUI M-file. However, if you think you may want to define a callback for the list box at some time, it is simpler to leave the callback stub in the M-file.
List Box Examples
See the following examples for more information on using list boxes:
Frames | Pop-Up Menus |