Loading a ComboBox List

A ComboBox presents a list of values in a dropdown box on a form. The values are for the underlying formfield. For example, the following form specification file contains a ComboBox that represents the formfield customer.state:
01 SCHEMA custdemo 
02 LAYOUT
03 GRID 
04  {
05   Store #:[a0  ]
06      Name:[a1                 ]
07     State:[a5      ]
08  }
09  END -- GRID
10 END
11 TABLES customer 
12 ATTRIBUTES
13  EDIT a0=customer.store_num; 
14  EDIT a1=customer.store_name;
15  COMBOBOX a5=customer.state;
16 END

During an INPUT, INPUT ARRAY or CONSTRUCT statement the ComboBox is active, and the user can select a value from the dropdown list. The value selected will be stored in the formfield named customer.state.

Getting a reference to the object

The ComboBox class contains methods that manage the values for a ComboBox. In order to use these methods you must first obtain a reference to the ComboBox object:

  • Define a variable for the reference to the ComboBox object. The data type for the variables is the class identifier (ui.ComboBox):
     DEFINE cb ui.ComboBox 
  • Open a form that contains a ComboBox using OPEN WINDOW ... WITH FORM:
     OPEN WINDOW w1 WITH FORM ("testcb")
  • Next, get a reference to the ComboBox object using the method provided. As a class method, this method is called using the class identifier. Provide the name of the formfield to the method:
     LET cb = ui.ComboBox.forName("customer.state")

Once you have a reference to the ComboBox object, you can call any of the methods defined in the class as object methods:

  • To add an item to a ComboBox list

    You can instruct the ComboBox to store a code (the name) in the formfield that the ComboBox represents, but to display the description (the text) in the list to help the user make his selection. For example, to store the value "IL" (name) in the formfield, but to display "Illinois" (text) to the user:

    CALL cb.additem("IL", "Illinois")
    

    If text is NULL, name will be displayed.

  • To clear the list of all values
    CALL cb.clear()
    
  • To remove an item from the list; provide the name
    CALL cb.removeitem("IL")
    

See the The ComboBox class documentation in the Genero Business Development Language User Guide for a complete list of the methods.

Adding values to the ComboBox from a Database Table

An example in Tutorial Chapter 5 GUI Options loads a ComboBox with static values. The following example retrieves the valid list of values from a database table (state) instead:

Example 6

01 SCHEMA custdemo 
02 MAIN
03  DEFINE cb ui.ComboBox 
04  CONNECT TO "custdemo"
05  OPEN WINDOW w1 WITH FORM "testcb"
06  LET cb = ui.ComboBox.forName("customer.state")
07  IF cb IS NOT NULL THEN
08   CALL loadcb(cb)
09  END IF
10 ...
11 END MAIN
12
13 FUNCTION loadcb(cb)
12  DEFINE cb ui.ComboBox,
13         l_state_code LIKE state.state_code,
14         l_state_name LIKE state.state_name 
15
18  DECLARE mycurs CURSOR FOR 
19    SELECT state_code, state_name FROM state 
20  CALL cb.clear()
21  FOREACH mycurs INTO l_state_code, l_state_name 
22  -- provide name and text for the ComboBox item 
23   CALL cb.addItem(l_state_code,l_state_name)
24  END FOREACH
26 END FUNCTION

Figure: Loaded combobox


This figure is a screenshot of a ComboBox loaded with data from the state table.

As an alternative to calling the loadcb function in your BDL program, this function can be specified as the initializer function for the ComboBox in the form specification file. When the form is opened, The initializer function is called automatically and a reference to the ComboBox object is passed to it. Provide the name of the initializer function in lowercase:

ATTRIBUTES 
COMBOBOX a5=customer.state, INITIALIZER = loadcb;