RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
C++

Save Time With This Quick LiveBindings Trick For Delphi And C++Builder

LiveBindings is a great technology built into Delphi and C++Builder for bypassing most of the code involved in a CRUD data cycle (Create, Read, Update and Delete Cycle). It can keep you out of massive amounts of technical debt. However, sometimes the standard LiveBindings method of binding some controls can be overly complex.

You can easily simplify your LiveBindings by using an invisible TEdit control. Basically you bind your field value to the TEdit and set the TEdit to Visible := False;. In the OnValidate event of the TEdit control you add code to update the REAL control you want to update (like a TTMSFMXSlider for example). In the OnChange event of the target control you send the updated value BACK to the invisible TEdit control and LiveBindings takes care of the rest. The TEdit control is bidirectional by default and will bind directly to a lot of field types. Whereas if you try to LiveBind directly to a Set, Enumeration, or SubRange it can be more difficult than it should be.

There are a lot of different ways to do this in LiveBindings but so far I am found this to be the easiest way. If you’ve got code snippets that do this easier (like using TLinkControlToField.AssigningValue or TMethodUtils.RegisterMethod) I’d like to hear about them so put them in the comments!

The demo I am providing over on GitHub shows how to bind a TFDMemTable field to a TEdit which updates a TTMSFMXSlider and is also bound to a TTMSFMXLiveGrid. You can change the switch, change the value in the TEdit, or change the value in the grid and all of the other values will automatically update. The code that pumps the switch change back to the TEdit is TLinkObservers.ControlChanged(SliderEdit); so you need to run this function to lock in the change. Here is the full code snippet from the demo but the real magic takes place in LiveBindings (the link to the demo source code is down below):

Note: The demo uses TMS FMX UI Pack (don’t leave home without it).

 

Head over to GitHub and download the full source code demonstrating this technique of using an invisible TEdit to easily LiveBind almost any control and value together in RAD Studio 10.2 Tokyo!


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Learn More About Upgrading

About author

FMXExpress.com has over 600 articles with all kinds of tips and tricks for Delphi FireMonkey on Android, IOS, OSX, Windows, and Linux.
Related posts
CodeDelphiRAD Studio

Learn How To Use FireDAC In The Dynamic-Link Libraries With DLL Sharing Sample In Delphi

CodeDelphiRAD StudioTech Partner

Powerful Cross Platform TeeGrid Is Capable Of Handling A Large Number Of Cells In Delphi FireMonkey

CodeDelphiRAD Studio

Learn How To Use Connection Pooling With A Multi-threaded Environment In Delphi

CodeDatabaseDelphiRAD Studio

Quickly Share Data Between Datasets Using CloneCursor In This Native Windows Sample For Delphi

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES