views:

270

answers:

1

I have a C# custom control that loads images from Resources.resx. I was loading this resources into the Project's Resources and then accessing them like:

ProjectNamespace.Properties.Resources.resourcename;

This works for one project but now I want to use my control in multiple projects.

What's the best way to handle this? Load the resources into the controls .resx? How can I access them from there? Or should I approach this completely differently?

+1  A: 

It should work as is, even if your control is used from other projects.

The code generated by VS is a wrapper around the ResourceManager class, and it gives the assembly of your control as a constructor parameter. So, the ResourceManager always knows where to look for resources.

Timores
@Timores, thanks. My trouble is that "ProjectNamespace" would change for each project I include my control in. I'm adding my control as a "link" into a project, not as a reference since I want it built into the DLL. I'm not sure how to get "ProjectNamespace" dynamically.
Mark
I see. You have to forget about the VS-generated code in that case and create an instance of ResourceManager yourself, giving this.GetType() as a parameter. Then to access a string called resourcename, call the GetString method directly (or the other methods of ResourceManager for other types).Better, create a small helper class, that mimics the one of VS. What matters is that it receives the Type of your control in order to get at the right namespace.
Timores
@Timores, spot on, this worked like a charm.
Mark