I want to duplicate a couple of container controls between a couple of Windows. And I don’t just mean using the same super. I want to really use the same object so that when I update an item in one of the containers, it’s updated on the others.
I could certainly store references to the containers and manually update each of the controls in the references. But I wanted to see if there was an easier way to do it. I have a habit of doing things the difficult way!
I want to duplicate a couple of container controls between a couple of Windows. And I don’t just mean using the same super. I want to really use the same object so that when I update an item in one of the containers, it’s updated on the others.
I could certainly store references to the containers and manually update each of the controls in the references. But I wanted to see if there was an easier way to do it. I have a habit of doing things the difficult way! :)[/quote]
I believe that you need an instance of the object for every instance of the control.
You may be able to mimic this using observers. To the user it will look like it is the same object, but behind the scenes the updates are pushed through your observer system and updated in each of the container controls. Check out the observer pattern example in Examples -> Design Patterns -> Observer
I want to duplicate a couple of container controls between a couple of Windows. And I don’t just mean using the same super. I want to really use the same object so that when I update an item in one of the containers, it’s updated on the others.
I could certainly store references to the containers and manually update each of the controls in the references. But I wanted to see if there was an easier way to do it. I have a habit of doing things the difficult way! :)[/quote]
Let’s say for instance, you are trying to keep the text the same of each instance of a textfield.
You can do the following:
Create a class called ‘TextFieldInstance’ of type ‘TextField’.
Create a class which holds the string value of the text called ‘TextFieldTextHolder’ (create a ‘value’ property of type string to hold the value)
Create an event called ‘TextInstance’ of return type ‘TextFieldTextHolder’ in the TextFieldInstance class
Add an instance of TextFieldInstance to the two windows with the same text values
Create a module called ‘MyTextHolders’ or whatever you wish
Create a property for each textfield which you want to have the text update for
Create a method that you will call somewhere, for example, in the window’s or the app’s open event (call this method InstituteMyTextHolders’.
In this method assign each property to a new instance of TextFieldTextHolder.
Create a timer and set it off at an interval of 1.
Use a delegate to define the Action event for the timer
In the delegate write ‘if me.text <> TextInstance.Value then me.text = TextInstance.Value’
For each instance of TextFieldInstance add an event and write ‘return MyTextHolders.Holder1’.
There may be some bugs with this and some serious optimizations can be made but I tried giving you the general idea.
I know how I could write code to do this all and I’ve done it with single controls in another part of my project. Just was hoping I could do it an easier way. But let me look at the observer example. Might be useful.
What you actually need is a Notification Center. I started using this in my latest project and can’t believe how I’ve been coding without it for the last 5 years.
[quote=143740:@JrmieLeroy]What you actually need is a Notification Center. I started using this in my latest project and can’t believe how I’ve been coding without it for the last 5 years.
So you mean I should write something that is cross platform. So I write a class that uses the Notification Centre for OS X and mimics it for other platforms?