Keeping GUI data elements in sync?

Say I have a Person class which contains a FirstName and LastName property. I then have a window showing people in a ListBox. Double clicking a person will open a new window to edit that person, it will contain two text fields. Now, when I edit the FirstName and press the Save button, what is the standard way to let the ListBox containing the person’s full name that things in the object have changed and it should update itself?

I originally created an event definition on the Person class named Updated. In limited testing this worked fine but then I found you could only bind to the Update event from one location. For example, say you add a third window that adds children to that person and in that window you have a static label showing the person’s full name. When editing that person in the Editor dialog you would want their name to update in the search results listbox as well as any other open window currently using that given person class.

Any hints and tips on how to accomplish this in Xojo? The only thing I can think of right now is my own event system where it keeps an array of delegates that want to be notified when the class is updated. Maybe I am missing something?

Recap:

Search Results (ListBox) contains many people displaying, one displaying “John Doe”

Person Editor dialog containing two TextFields editing “John” and “Doe” of the double clicked Person class

Another window opened from the Search Results of that same person, maybe showing “John Doe” in a Label widget providing the means to add children to that person.

When the editor is Saved, I’d like the Search Results ListBox to update with the new name as well as the “Child Editor Window’s Person Label” to update. i.e. the same class used in various places throughout my app. This is just a convoluted example, I wouldn’t make a person editor like that but in my real app things are much more complex, I have a Client class that is used in all sorts of various places and when the client is updated, I want everyone who is using the Client object to be able to update themselves.

Thanks!

Use a class interface…

Check out the Observer example in the Design Patterns example folder.

actionSource / actionNotificationReceiver and dataNotifier / dataNotificationReceiver class interfaces (built in) are similar.

Peter, I literally was typing a reply as yours popped up. That is exactly what I did. I missed the example, I really should pay more attention to them there are some real gems there, but I created not an interface but a base class with three methods AddObserver, RemoveObserver and NotifyObservers. When the object is saved, it simply calls NotifyObservers and the magic is done.

I am, though, going to look at the example now and see how things differ. Thanks for the push back to the examples!

The interface allows you to process your changes for any class which uses it.

Depending on your windows (forms) you can update textfields, buttons, popupmenus, comboboxes, custom classes, etc, etc, as long as they implement the interface.

Peter, it seems that the example Observer pattern is window centric. In my case, I have objects that were loaded from a remote source. They are cached locally when needed and they can be changed by any number of windows in the application or even by the remote source pushing updates. Thus, I made the observer pattern centric to the objects containing the data. When a window then makes use of that object, it registers itself as an observer and knows how to update its GUI when the object data changes. When the window is done with that object, it removes itself as an observer.

It’s always cool seeing multiple solutions to the same problem.

Agreed :slight_smile: You may like this as well

link EventBusExample

Peter

EventBusExample link seems brocken.