Calculating fields together with LiveBindings in Delphi FireMonkey can be a good low code alternative to writing a bunch of code to do the same. LiveBindings makes it relatively easy to add fields together using it’s CustomFormat property. LiveBindings supports a wide variety of operations in the CustomFormat property can you can check out more information about that in the Embarcadero DocWiki. There is a demo with this post that shows how to add three fields together and output the result to a fourth field. You should be able to use the demo cross platform on Android, IOS, OSX, and Windows. The demo uses components from the TMS FMX UI component set but the components are probably not required if you implemented these ideas in your own app. There is a TFDMemTable in the project with 4 fields called FieldEdit1, FieldEdit2, FieldEdit3, and FieldEdit4. The four fields are bound with LiveBindings to 4 edit controls called Edit1, Edit2, Edit3, and Edit4. You can also see the contents of the TFDMemTable in the featured TTMSFMXLiveGrid. Entering number values into any of the Edit1, Edit2, and Edit3 controls will add those 3 values together and give you a final value in the Edit4 control. You can see that when you add values into the Edit1, Edit2, and Edit3 controls that the value is automatically updated in the TFDMemTable. Finally, pressing the included Save button will store the calculated value from the fourth Edit4 control into the TFDMemTable as well. In the calculation demo the LinkControlToField1.CustomFormat property is set as the following:
IfThen(Self.Owner.FieldEdit1.AsString=”, 0, Self.Owner.FieldEdit1.AsFloat)+
IfThen(Self.Owner.FieldEdit2.AsString=”, 0, Self.Owner.FieldEdit2.AsFloat)+
IfThen(Self.Owner.FieldEdit3.AsString=”, 0, Self.Owner.FieldEdit3.AsFloat)
Note: Self.Owner.FieldEdit1, Self.Owner.FieldEdit2, Self.Owner.FieldEdit3
are the names of the fields in the TFDMemTable.
The reason it uses the IfThen() function is to keep any conversion errors from happening. If it’s blank use zero otherwise use the field value. The TTMSFMXEdit is setup to only allow number characters.
LiveBindings is built into Delphi and C++Builder so you can use it out of the box in all of your latest applications.
Download the full demo of the LiveBindings calculation example for Delphi Firemonkey on Android and IOS