Tutorial Chapter 12: Changing the User Interface Dynamically |
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.
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 cb ui.ComboBox
OPEN WINDOW w1 WITH FORM ("testcb")
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:
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.
CALL cb.clear()
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.
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:
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 1. Loaded combobox
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;