Duplicate A Container Control

Hey guys,

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! :slight_smile:

[quote=143729:@Jon Ogden]Hey guys,

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

[quote=143729:@Jon Ogden]Hey guys,

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:

  1. Create a class called ‘TextFieldInstance’ of type ‘TextField’.
  2. Create a class which holds the string value of the text called ‘TextFieldTextHolder’ (create a ‘value’ property of type string to hold the value)
  3. Create an event called ‘TextInstance’ of return type ‘TextFieldTextHolder’ in the TextFieldInstance class
  4. Add an instance of TextFieldInstance to the two windows with the same text values
  5. Create a module called ‘MyTextHolders’ or whatever you wish
  6. Create a property for each textfield which you want to have the text update for
  7. Create a method that you will call somewhere, for example, in the window’s or the app’s open event (call this method InstituteMyTextHolders’.
  8. In this method assign each property to a new instance of TextFieldTextHolder.
  9. Create a timer and set it off at an interval of 1.
  10. Use a delegate to define the Action event for the timer
  11. In the delegate write ‘if me.text <> TextInstance.Value then me.text = TextInstance.Value’
  12. 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.

Let me check that out.

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.

This subject is very well explained in xDev release 12.5, check the article “* In-App Notifications * by Sam Rowlands”:
https://forum.xojo.com/15554-xdev-magazine-releases-issue-12-5-september-october-2014-and-an/0

Using observers is probably a better approach.

[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.

This subject is very well explained in xDev release 12.5, check the article “* In-App Notifications * by Sam Rowlands”:
https://forum.xojo.com/15554-xdev-magazine-releases-issue-12-5-september-october-2014-and-an/0[/quote]

Just downloaded this. Looks to be a good way to do it.

The notification center is a design pattern you can use and even create entirely in Xojo code so its cross platform.

How do you use the notification centre? Is it free?

Read the article ?
Notification center is built into OS X

What about Windows and Linux? Thanks

Oliver the article explains it all and uses pure Xojo code without any declares so it is completely cross-platform.

I use it extensively in my latest app that is running on MacOS and Windows. I don’t believe there should be any problems for Linux.

I don’t have the article so I wasn’t sure what Sam used
Nice to know he did it entirely in Xojo as it’s really a nice pattern

[quote=143768:@JrmieLeroy]Oliver the article explains it all and uses pure Xojo code without any declares so it is completely cross-platform.

I use it extensively in my latest app that is running on MacOS and Windows. I don’t believe there should be any problems for Linux.[/quote]
Free?

Thanks

No its not free, you have to purchase the article.

http://en.wikipedia.org/wiki/Notification_Center
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Notifications/Introduction/introNotifications.html

Oliver, it’s honestly not hard to set this up. If you put your mind to it you might be able to finish it in less than an hour.

http://en.wikipedia.org/wiki/Observer_pattern

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?

Thanks