Sharing User Control Over Multiple Windows

monserob
monserob used Ask the Experts™
on
I have a user control that displays billing information and this control has been added to more than one window. The information displayed in each instance of the user control is identical.

Rather than updating different instances of the user control how can I "share" the information so that updating it once updates all the different screens?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015
Commented:
You can share the fields (variables declared in the Declaration section of the UserContol) as Public Shared, and these variables will be shared by all your instances.

You cannot do the same for the controls that you use to display the data however.

The only way I see (maybe someone will have a better idea) is to record any change in one of your controls in such shared variables. When the user switch to a form or tab that has another instance, simply call a method that transfer the content of the variables into the controls.
Most Valuable Expert 2012
Top Expert 2014
Commented:
Winforms right? One option is to create a custom event on the main form (which displays all other forms). Then subscribe to that event in each user control. When you need to update these controls, raise that event with your data and all controls will catch the event and update themselves.

Author

Commented:
I ended up creating a class to hold the billing information and containing it within a class that can probably best be described as a shared global variable. The controls in the user control are bound to this class and updated using an event. I've used ideas from both solutions so I'm going to accept both.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial