XE3 Visual LiveBindings: Link a field to a lookup list
A lookup list is used when the actual data values are not meaningful to users. A lookup list populates a list control with display values, instead of actual data values.
We can configure two TPrototypeBindSource components to represent a situation that calls for a lookup list. These steps differ from the previous post by using a Colors field in addition to a ColorsName field. The Colors field holds a numeric color value.
First, create the "DataSource".
- New VCL application
- Drop a TPrototypeBindSource on the form
- In the object inspector, set PrototypeBindSource1.RecordCount to 10
- Add a Colors field to the TPrototypeBindSource
Next, create the "FillDataSource"
- Drop a second TPrototypeBindSource on the form
- In the object inspector, rename to "PrototypeBindSourceFill"
- Add Colors and ColorsNames fields to the PrototypeBindSourceFill
- Uncheck "Shuffle" and "Repeat" to generate an ordered list of unique values
Drop a TListBox on the form
Choose the "View/LiveBindings designer" command to show the LiveBindings designer:
For clarity, rename PrototypeBindSourceFill fields to AColor and AColorName. Select each field in the LiveBindings designer and use the object inspector to type in a new name:
In order to create a lookup list, the "FillDataSource" must provide a "ValueField" and a "DisplayField". These two fields are used to build a dictionary which can be used to lookup a display value for each actual value.
In this example, AColor is the "ValueField" and AColorName is the "DisplayField". A numeric color value in AColor can be used to lookup a color name in AColorName.
Make three connections in the LiveBindings designer to create a lookup list:
- Connect "PrototypeBindSource1.Color1" to "ListBox1.SelectedText"
- Connect "PrototypeBindSourceFill.AColorName" to "ListBox1.ItemText".
- Connect "PrototypeBindSourceFill.AColor" to "ListBox1.LookupData".
ListBox1 is populated in the form designer:
"Darkgreen" is selected because the value of the "Colors1" field in the first record of PrototypeBindSource1 is paired with "Darkgreen" in PrototypeBindSourceFill.
Right click on PrototypeBindSource1 and choose "Add Navigator". Now the app is ready to run.
I've also added a TLabeledEdit and a TShape.
The TLabeledEdit shows the value of PrototypeBindSource1.Color1 as a number.
The TShape shows the value of PrototypeBindSource1.Color1 field as a color.
Here is the complete design:
The source for this sample is available here. See LookupListBoxVCL.dpr.
Please login first in order for you to submit comments
- Page :