Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring lists using LiveBindings

I've been trying to find a generic way to notify a UI control of changes in a list. For example, when an object is added to a list I want it added automatically to the listbox. If an object is removed from the list, I want it removed automatically from the listbox.

Unfortunately there are some maddening inconsistencies between Delphi's lists: TList<>.Notify is fine but TStringList.OnChange doesn't even tell you what the change was and TList doesn't even have any notification whatsoever!

I was hoping LiveBindings would give me a proper way of monitoring changes in most lists, but so far I don't see it. All the examples I have seen fills the control and sets up bindings between the list items and the control items (listbox items in my example). But adding/removing an object from the list doesn't affect the control at all.

Does anyone know of a mechanism that I have missed, or more generally, a nice generic way to do this?

Clarification: It doesn't seem that I was clear enough before. The reason I need a generic way, is because I wrote a grid control that can connect to a variety of sources, including some existing code. I wrote an interface that the grid accepts as source and then a number of adapter classes to accept some lists and make them available as that interface. Since I needed to accept existing code as well, overriding TList.Notify is not an option. And since there is no event to see the changes, it effectively means TList does not have a notification mechanism that is usable by a client, such as my adapter class. TStrings doesn't have one either, but TStringList raises a simple OnChange, which means that the adapter class cannot actually determine what has changed.

I actually had a very nice solution that used the TVirtualMethodInterceptor, but that completely stopped working in Delphi XE and its not fixed in XE2.

like image 483
Cobus Kruger Avatar asked Sep 13 '11 08:09

Cobus Kruger


1 Answers

You missed the TList.Notify mechanism.

TList has a protected Notify procedure that you must override. This is the way TObjectList works.

like image 105
The_Fox Avatar answered Sep 28 '22 04:09

The_Fox