Summary:
See also: Classes and Objects, Form Specification File
The ComboBox class provides an interface to the COMBOBOX form field view in the Abstract User Interface tree.
ui.ComboBox
Class Methods | |
Name | Description |
forName( fieldname STRING ) |
Returns the combobox object identified by fieldname in the current form. |
setDefaultInitializer( funcname STRING ) |
Defines the default initialization function for all COMBOBOX form fields. See also the INITIALIZER attribute in form specification files. |
Object Methods | |
Name | Description |
clear() |
Clears the list of combobox items. |
addItem( name STRING, text STRING ) |
Adds an item to the combobox item list. |
getColumnName() |
Returns the name of the column name of the form field associated with this combobox. |
getIndexOf(
name STRING ) |
Returns the position of a given item by name. |
getItemCount() |
Returns the current number of items defined in the combobox . |
getItemName( index INTEGER ) |
Returns the name of an item at a given position. First is 1. |
getItemText( index INTEGER ) |
Returns the text of an item at a given position. First is 1. |
getTableName() |
Returns the name of the table, alias or FORMONLY of the form field associated to this combobox. |
getTag() |
Returns the user-defined tag of this object. |
getTextOf( name STRING ) |
Returns the text for a given item name. |
removeItem( name STRING ) |
Removes the item identified by name. |
When you declare a COMBOBOX form field in the form specification file, you declare both a form field and a view for that model. The ComboBox class is an interface to the view of a COMBOBOX form field.
Use the ui.ComboBox.setDefaultInitializer()
class method to
define the default initialization function that will be called each time a
ComboBox object is created. That function is called with the ComboBox object as
the parameter:
01
CALL ui.ComboBox.setDefaultInitializer("initcombobox")02
03
FUNCTION initcombobox(cb)04
DEFINE cb ui.ComboBox05
CALL cb.clear()06
CALL cb.addItem(1,"Paris")07
CALL cb.addItem(2,"London")08
CALL cb.addItem(3,"Madrid")09
END FUNCTION
Note that you must specify the initialization
function name in lower-case letters to the setDefaultInitializer()
method. The BDL syntax allows case-insensitive function names, but the runtime
system must reference functions in lower-case letters internally.
You can also define a specific initialization function in the form specification file, by using the INITIALIZER attribute (as shown in the second example below).
The ui.ComboBox.forName()
class method searches for a COMBOBOX object by form field name in the current form.
Typically, after
loading a form with OPEN WINDOW WITH FORM,
you use the class method to retrieve a COMBOBOX view object into a variable defined as a ui.ComboBox
:
01
DEFINE cb ui.ComboBox02
LET cb = ui.ComboBox.forName("formonly.airport")
It is recommended that you verify if that function has returned an object, because the form field may not exist:
01
IF cb IS NULL THEN02
ERROR "Form field not found in current form"03
EXIT PROGRAM04
END IF
Once instantiated, the ComboBox object can be used; for example, to set up the items of the drop down list:
01
CALL cb.clear()02
CALL cb.addItem(1,"Paris")03
CALL cb.addItem(2,"London")04
CALL cb.addItem(3,"Madrid")
The clear()
method clears the item list. If the item list is
empty, the ComboBox drop-down button will show an empty list on the client side.
The addItem()
method
adds an item to the end of the list. It takes two parameters: the first
is the real form field value, and the second is the value to be displayed in the
drop down list. If the second parameter is NULL,
the runtime system automatically uses the first parameter as the display value.
Trailing spaces should be avoided when populating the first parameter because values get truncated when field validation occurs, and the resulting value (without trailing spaces) will no longer match the ComboBox item name. Additionally, trailing spaces in the second parameter may cause the ComboBox to be much wider than expected. In order to avoid such problems, use VARCHAR or STRING variables, or use the CLIPPED operator with CHAR variables.
The getTableName()
method returns the name of the form field
table prefix. Not that this prefix can be NULL if not defined at the form field
level.
The getColumnName()
method returns the name of the form field
table prefix. Not that this prefix can be NULL if not defined at the form field
level.
You can use the getTableName()
and getColumnName()
methods to get the table and column name of the form field associated with the
ComboBox:
01
DISPLAY cb.getTableName()||"."||cb.getColumnName()
The getTag()
method returns the value of the TAG
attribute if defined in the form file.
The getIndexOf()
method takes an item name as parameter and
returns the position of the item in the list. Returns 0 if the item name does not exist.
With this method you can check if an item exists:
01
IF cb.getIndexOf("SFO") == 0 THEN02
CALL cb.addItem("SFO", "San Francisco International Airport, CA" )03
END IF
You can get the current number of items defined in a ComboBox with the getItemCount()
method. If no items are defined, the method returns zero.
The getItemName()
method takes an item position as parameter
and returns the identifier of that item. First item starts at position 1.
The getItemText()
method takes an item position as parameter and
returns the value of that item. First item starts at position 1.
The getTextOf()
method takes an item name as parameter and
returns the value of that item. Returns NULL if the item name does not exist.
Use the removeItem()
method to delete an item from the ComboBox.
Item name must be passed as parameter.
Form Specification File:
01
DATABASE FORMONLY02
LAYOUT03
GRID04
{05
Airport: [cb01 ]06
}07
END08
END09
ATTRIBUTES10
COMBOBOX cb01 = FORMONLY.airport TYPE CHAR;11
END
Program File:
01
MAIN02
DEFINE cb ui.ComboBox03
DEFINE airport CHAR(3)04
05
OPEN FORM f1 FROM "combobox"06
DISPLAY FORM f107
LET cb = ui.ComboBox.forName("formonly.airport")08
IF cb IS NULL THEN09
ERROR "Form field not found in current form"10
EXIT PROGRAM11
END IF12
CALL cb.clear()13
CALL cb.addItem("CDG", "Paris-Charles de Gaulle, France")14
CALL cb.addItem("LCY", "London-City Airport, UK")15
CALL cb.addItem("LHR", "London-Heathrow, UK")16
CALL cb.addItem("FRA", "Frankfurt Airport, Germany")17
IF cb.getIndexOf("SFO") == 0 THEN18
CALL cb.addItem("SFO", "San Francisco International Airport, CA" )19
END IF20
21
INPUT BY NAME airport22
23
END MAIN
Form Specification File:
01
DATABASE FORMONLY02
LAYOUT03
GRID04
{05
Airport: [cb01 ]06
}07
END08
END09
ATTRIBUTES10
COMBOBOX cb01 = FORMONLY.airport TYPE CHAR, INITIALIZER=initcombobox;11
END
Initialization function:
01
FUNCTION initcombobox(cb)02
DEFINE cb ui.ComboBox03
CALL cb.clear()04
CALL cb.addItem("CDG", "Paris-Charles de Gaulle, France")05
CALL cb.addItem("LCY", "London-City Airport, UK")06
CALL cb.addItem("LHR", "London-Heathrow, UK")07
CALL cb.addItem("FRA", "Frankfurt Airport, Germany")08
CALL cb.addItem("SFO", "San Francisco International Airport, CA" )09
END FUNCTION