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:
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.
FMXExpress.com has over 700 tips and tricks for building cross platform apps on Android, IOS, OSX, Windows, and Linux with a single codebase and single UI using the FireMonkey framework found in Delphi and C++Builder.
FMXExpress covers top cross platform secrets for FireMonkey in C++Builder and Delphi XE5, XE6, XE7, XE8, Seattle, Berlin, Tokyo, Rio, and Sydney.