How to use Resource files in VS 2005?

T

Tom

This may seem a little strange, but in all my years of developing Windows
Forms apps, I've never really used Resource files; however, I would like
to start doing so in order to store icons, pictures, and most importantly,
error message strings in resource files so I don't have to go searching for
them inside the code (if I want them changed).

I know VS 2005 makes it MUCH easier to use and create resource files, but
in my few times I have tried I ended up not doing things right. Therefore,
does anyone know of any articles, tutorials, etc on how to createm use, and
ACCESS resource files (and items in them) in VS 2005? I am sure it is not
hard but a tutorial or something would go a long way in helping out.

Thanks a bunch.

To
 
A

Andrew Backer

Well, it depends on what you want to do with it. I wasn't able to find
many good tutorials, but here is kinda what we use.

Go to 'add new item' and choose resource file, name it
MyResources.resx. VS will generate some code for this, but there are
ways to load the resource manager by hand. You can 'show all files'
on the project, and then look at the code it generates for the .resx
file to see how some of this works.

To get at a resource : MyResources.MyResources.res_name (returns
string/icon/etc)
To get the manager : MyResources.MyResources.ResourceManager

Loading it dynamically is a little harder, but not too bad. You just
have to know what assembly it is in, mostly. I wrote a utility class
that sits in the same dll as the resources, so I get my manager by
calling :

Dim resMgr as New ResourceManager(
GetType(IconResources.IconResources).Name,
Assembly.GetExecutingAssembly
)
// you could also use GetType(...).Assembly there.

After that you can call GetObject/GetResource on it, wrap it up with
nice functions, etc. Do this is if you need to have someone specify
which resource manager to return by name, not type. Also, look in the
code for the resx to see another way of loading the manager

So far it works pretty well for me. I can not get access to this
resource file from another dll (in designer mode) but it works great
for all my dynamic stuff.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top