Jump to content
arend

Custom TListBoxItem Live Bindings Advice

Recommended Posts

This might be a bit long-winded but basically I'm looking for some pointers for developing a new project.

 

I am using RAD Studio 11, using both Delphi and C++ Builder, although I have more experience in C++.

 

Previously I have been downloading data from a JSON API, processing the JSON into separate objects (mainly just containing strings), adding those objects to a TList and displaying them using a custom TListBoxItem object in a TListBox.  This works fine but is slow to process, save, load, filter and display (i.e. need to create a new TListBoxItem for each object and populate manually, and save/load from a custom text file format).  For future projects I would like to use something like a TClientDataSet or TFDMemTable but don't have much experience with them.

I have gotten to the point where I can populate a TFDMemTable with arbitrary JSON downloaded from my API and displaying that using design time LiveBindings on a TStringGrid.  

 

My query boils down to how I would display my data using a TListBox with much more information than the default TListBoxItem can show (i.e. more than two strings, multiple icons/pictures, etc.), or if there is another better/easier way to handle this.

Would I create my custom TListBoxItem object and bind it programmatically somehow?  Use a style book to create a custom layout?  Some other process I'm not aware of?

 

I don't have much experience with the database objects in RAD Studio or the live bindings but this seems like the way to go to make my applications snappier and quicker for users.  Any pointers would be much appreciated.  

Share this post


Link to post

First, you should choice between VCL or FMX frameworks!

  • each one have your propose distinct for generate your GUI, same than the components have a similar appearance, including some properties/methods!
  •  
  • VCL to MSWindwos only! = 1 platform
    • basically, this the MSWindows world, then, think in this world!
  • FMX to MSWindows, Linux, macOS, (mobile Android, iOS ) = multiplatform!
    • more flexible components usage!
    • basically, any component can be a container to anothers components! then, you can put a "TImage" in your TListbox item, or TLisView item for example!
    • currently, games are created using this framework, because your caracteristics to draw your graphics on screen
      • of course, a skilled developer will question this point, as a lot can also be done in VCL and third party libraries!!!
  • at general, think that "two frameworks distinct in much aspects", but with similar propose!
    • at general, do mix two propose!

after this, you can choice how to show your list?

  • TListBox = vertical arrangement of data, in 1 colum
  • TListView = vertical arrangement of data, in more than 1 column (using property ViewStyle)
  • at general, two components has its indications of use, for example:
    • TListView used used to display a much larger amount of data than TListbox... (now what is "large amount of data", it's up to you to define it)
  • any way, both components are not data aware, that is, they are totally dependent on the "LiveBinding" or any other means that the programmer wants to interact with the data on the screen

Now, between TClientDataset and FDMemTable, for sure, FDMemTable it is much more flexible, and the FireDAC framework works practically with any current data source, whether using Database driver, ODBC (for text files, csv, Excell, etc...), you will even find it very easy to use for the pretense of using JSON, although this is not the focus of FireDAC! But there are classes to make the job easier.

  • FDMemTable works natively with XML, JSON, FDS (FireDAC in binary mode) - saving or reading data from a dataset

 

Whether for MSWindows or other platforms, you can choose FireMonkey without fear of making a mistake!

Edited by programmerdelphi2k

Share this post


Link to post

Thanks for that breakdown, but I was looking for some more specific information. I guess I should have been more specific.

 

I am currently using FMX as I am creating cross platform applications for PC and mobile.  Mainly I would like to know how (or if) I can use an FDMemTable with custom ListBox items.  Currently I am defining a custom TListBoxItem class with internal variables, procedures and functions, and then individually instantiating them manually to populate from a list of data.  I am just looking for pointers of ways to do this using live bindings or some other method that doesn't require me to loop through a list and populate manually, as this makes my applications quite sluggish.

 

I have looked at:

  • Programmatically live binding my table to my custom TListBoxItem object but haven't been able to get this working, not even sure if it is possible.
  • Using a style book to define a custom list box item style, which I can get to work but is rather limiting.  Some of my list box items have parts that users can click to expand/contract and other functions that don't seem possible with this method.

Maybe I'm not understanding live bindings correctly or am missing some method that will let me do what I need.  Hope that gives more insight into my query.  Thanks again.

Share this post


Link to post

ok, now I think that understand a little more your question!

first, basically, all components in FMX is not data aware, like in VCL (DBEdit, DBGrid, etc...)

all is done using LiveBinding tech, or by code!

  • create a LiveBinding by code it's very complicated, but it's possible, of course!
    • basically, you'll use a TAdapterBindSource component to create your "LOAD" data!!!
      • you can use yourself "Class" to read/store your infos, like TPerson ... FName, FLastName, etc...)
      • naturelly, you'll have create your logic to updates, saves, read, etc...
  • now, to create your LiveBinding "to binds controls", it's another history... depend of your "necessity" or "complexity"
    • here, it's a part complicated to "open" this black-box
    • really can needs much code
  • BindSourceDB FMX is your DataSource in VCL!
  • You'll note that there is not code using LiveBinding because it works like a "Observer Pattern", send and receiving messages

Regarding the use of "styles", you can either use the "Bitmap Style Designer" (Tools menu) to create your styles, or you can do it all in code (needless to say it will be painful)

Basically, the styles mechanism works as a proxy, that is, the property definitions are passed to the final components in a trust relationship, that is, there is compatibility between the object that defined the style and the object that will receive the style.

 

In FMX, a style is not the same thing as a "SKIN" (something purely visual), for example!


A style is, firstly, "a behavior" (by having a reaction to a user action), and, secondly, a visual appearance!


But it's not something out of this world, however, it can be quite complicated to do everything in code!


Be aware that the engine that applies the style, looks for some keywords, and that it cannot be translated into other languages, for example! Ex. the name of an object inside style, must be unique, because it will be the one that will receive the updates.

 

Share this post


Link to post

So in this instance what would you suggest?  How can I get a list box filled with a custom TListBoxItem using live bindings or similar method?   As a very basic example, here's how I currently do it:

// create a custom TListBoxItem class (this one will only have one button but mine have multiple layouts, buttons, animations, etc.)
class TListBoxCustom : public TListBoxItem
{
__published:
  TButton *FButton;
  __fastcall TListBoxQueue(TComponent *Owner)
  {
    FButton = new TButton(this);
    FButton->Parent = this;
  };
}

// when I need to populate a list box i loop and instantiate:
for(int i = 0; i < 100; i++)
{
  TListBoxCustom *item 		= new TListBoxCustom (parent);
  item->Parent			= parent;
  item->FButton->Text 		= (UnicodeString)i;
  item->FButton->OnClick	= ClickHandler;
}

If I do live binding at design time I see no way of binding to custom list box items, either using style books or other methods, and I'm not sure at all how to do it at run time.  Is it possible at all or do I need to stick with my current slow method?

Share this post


Link to post

Don't you think you can use FMX.Tlistview (dynamic appearance) instead of FMX.Tlistbox ?

I wrote many (french) notes here or tutorials (here) about

Share this post


Link to post
12 hours ago, Serge_G said:

Don't you think you can use FMX.Tlistview (dynamic appearance) instead of FMX.Tlistbox ?

I could but have found that to be quite limiting compared to the custom TListBoxItem, i.e. if I wanted edit boxes, combo boxes, animations, drag-drop functionality, etc.

Share this post


Link to post
On 12/8/2022 at 10:43 PM, arend said:

if I wanted edit boxes, combo boxes, animations, drag-drop functionality,

Ah, ok, in this case TListBox (or a <Vert,Horz>ScrollBox with Frames) is the need.

 

When XE4, ListView and dynamic appearance does not exist, as neophyte to styles I wrote my experience in this paper.

I think this chapter will cover your needs

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×