Skip to toolbar
Skeeze - Pixabay

Preface

One of the very nice things about users is that they sometimes request features that I did not originally think about.

This blogpost explains a new one of those, namely binding to TStrings (including TStringList and other TStrings descendants). The shown features will be available in the upcoming release of kbmMW.

Binding to a TStringList

First let us see what it can do, then I will show how to do it:

So how is it done?

First we create a TStringList:

     FStrings:=TStringList.Create;
     FStrings.AddObject('Name1=Item 1',TObject(1000));
     FStrings.AddObject('Name2=Item 2',TObject(2000));
     FStrings.AddObject('Name3=Item 3',TObject(3000));
     FStrings.AddObject('Name4=Item 4',TObject(4000));
     FStrings.AddObject('Name5=Item 5',TObject(5000));
     Binding.DefineData('strings',FStrings);

I have also defined a placeholder for the TStringList instance, called strings. This makes it easy to access it later on, and even to replace it with another TStrings descendant on the fly.

This time I show how to bind from code, but using the textual expression method:

     Binding.Bind('{@strings.#strings, to:Edit10.Text, twoWay:true}',self);
     Binding.Bind('{@strings.#name, to:Edit11.Text, twoWay:true}',self);
     Binding.Bind('{@strings.#value, to:Edit12.Text, twoWay:true}',self);
     Binding.Bind('{@strings.#objects, to:Edit13.Text, twoWay:true}',self);

The four TEdit’s could have been bound up using designtime binding as have been shown earlier, or by binding specifically to the FStrings variable. But I do recommend using the placeholder technique since it makes it so easy to separate binding from data, and thus to replace data with something else.

As you probably have noticed, the source reference in the binding use a syntax like @strings.#strings. Obviously we want to bind between the placeholder strings (which refers to the FStrings:TStringList instance), and the Edit controls. To tell what part of the TStrings item we want to bind to, we refer to either TStrings’s properties directly, or what is more relevant in this case, we refer to a couple of specialized binding members, namely #strings, #name, #value and #objects.

#strings refers to that we want to bind to the complete strings value (name, separator and value if such are defined), for the current position by refered to by the bindings navigator.

#name refers to that we only want to bind to the name part.

#value refers to that we only want to bind to the value part.

#objects refers to that we want to bind to the objects property. It is treated as an integer or int64 value depending on if you are running on a 32 or 64 bit CPU. The reason for this, is that the Objects property often is used as a strings item integer tag value.

Remember… if you like kbmMW or what you read here, share it with your friends and colleagues.

Also remember you can get going totally for free by downloading kbmMW Community Edition, which can be used for teaching, personal use and even commercial use (terms apply). kbmMW Community Edition do not include source, and only supports latest version of Delphi in 32 bit mode, it  however contains most features found in kbmMW Enterprise Edition except those that require compilation of the kbmMW source code.

Happy binding

Kim/C4D

 14,703 total views,  2 views today

Leave a Reply

Your email address will not be published. Required fields are marked *

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