Monitoring lists using LiveBindings
Asked Answered
A

2

8

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.

Abseil answered 13/9, 2011 at 8:27 Comment(2)
TList has notifications, but you have to override the protected Notify method.Xylography
@Xylography This is the answer, at least for TList. You should write it as an answer.Griffith
X
4

You missed the TList.Notify mechanism.

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

Xylography answered 13/9, 2011 at 11:10 Comment(1)
Thank you for the answer, The_Fox, but my problem is that I don't control the code containing the list. Consequently I cannot override a virtual method to do what I want and the default TList.Notify does nothing. I need code external to the list to be able to monitor changes, which is why I hoped LiveBindings may provide a usable mechanism. I updated the description to accurately reflect my use case and why overriding a virtual won't work.Abseil
B
0

Bit late answer, but the generic TList has an OnNotify including the item added or removed.

Brewer answered 19/10, 2011 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.