views:

38

answers:

2

We have a winform app that has been in development for some time. It has serveral images and control colours that create a branded experience. We have a second customer who would like the same product, but with different branding.

So we have lines of code in the control designer.cs files like:

this.BackgroundImage = global::MyNameSpace.Properties.Resources.background;

It seems like resource files are the way to go, as I could simply change the culture and have a separate resource file with different images.

But I don't want to change the culture (I don't want the display of dates, currency, etc.) to be altered and it just smells.

Can I change the resource file manually at runtime or compile time without having to change the culture?

Is there a better way to achieve this outcome?

Also, how do you / can you use resource files to set controls colours (e.g. change all backgrounds from black to red)?

A: 

You can read in the resources from a separate file that just had the resources (but doesn't depend on the globalized satellite assembly mechanism). You could also compile default resources with your main assembly as a fall-back.

You can use Resgen.exe to create a .resources file from, e.g., a .resx file and then use ResourceManager to read it:

//note that resourceFilename does NOT include the ".resources" extension
var rm = ResourceManager.CreateFileBasedResourceManager( resourceFilename
                                                        ,resourceDir
                                                        ,null);
this.BackgroundImage = (Image) rm.GetObject("background");
Mark Cidade
Mark, that sounds promising. Can you point me to resource that can step me through creating and reading in resources from a separate assembly?
Handleman
I changed my answer since you just need the resources file without linking into an assembly. I included usage instructions + code.
Mark Cidade
Thanks for the assistance and code. This solution seems like I would have to change the code in the existing designer files fromthis.BackgroundImage = global::MyNameSpace.Properties.Resources.background; tothis.BackgroundImage = (Image) rm.GetObject("background"); and would have to have "rm" in scope for those files. Won't this produce and error when I load the control into the designer?
Handleman
I would remove the code from the designer file and put the custom code in the form's main file.
Mark Cidade
Your suggestion works, it just seems to be the high road from where I am. The code base that exists is large and refactoring it all to accommodate this change seems un-warranted since the alternative would be to simply add one line of code "Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR")". The only problem with setting the culture is the affect it will have on dates, currency etc. It just seems that there should be an easy way to leverage the ability that the culture setting has to swap the resources.
Handleman
A: 

One option you could consider is to create a custom culture, so for example you could create a culture called

ClientA-fr-FR
ClientB-fr-FR

The you create resources for each and you should be able to set the Thread Culture approriately.

Here is a link "How to: Create Custom Cultures"

Chris Taylor