views:

6733

answers:

3

How do I create a resource that I can reference and use in various parts of my program easily?

My specific problem is that I have a NotifyIcon that I want to change the icon of depending on the state of the program. A common problem, but one I've been struggling with for a long time.

+14  A: 

Well, after searching around and cobbling together various points from around StackOverflow (gee, I love this place already), most of the problems were already past this stage. I did manage to work out an answer to my problem though.

How to create a resource.

In my case, I want to create an icon. It's a similar process, no matter what sort of data you want to add as a resource though.

  • Right click the project you want to add a resource to. Do this in the Solution Explorer. Select the "Properties" option from the list.
  • Click the "Resources" tab.
  • The first button along the top of the bar will let you select the type of resource you want to add. It should start on string. We want to add an icon, so click on it and select "Icons" from the list of options.
  • Next, move to the second button, "Add Resource". You can either add a new resource, or if you already have an icon already made, you can add that too. Follow the prompts for whichever option you choose.
  • At this point, you can double click the newly added resource to edit it. Note, resources also show up in the Solution Explorer, and double clicking there is just as effective

How to use a resource.

Great, so we have our new resource and we're itching to have those lovely changing icons... How do we do that? Well, lucky us, C# makes this exceedingly easy.

There is a static class called Properties.Resources that gives you access to all your resources, so my code ended up being as simple as:

paused = !paused;
if (paused)
    notifyIcon.Icon = Properties.Resources.RedIcon;
else
    notifyIcon.Icon = Properties.Resources.GreenIcon;

Done! Finis! Everything is simple when you know how, no?

Matthew Scharley
+3  A: 

The above method works good.

Another method (I am assuming web here) is to create your page. Add controls to the page. Then while in design mode go to: Tools>Generate Local Resource. A resource file will automagically appear in the solution with all the controls in the page mapped in the resource file.

To create resources for other languages, append the 4 character language to the end of the file name, before the extension (Account.aspx.en-US.resx, Account.aspx.es-ES.resx...etc).

To retrieve specific entries in the codebehind, simply call this method: GetLocalResourceObject([resource entry key/name]).

Chuck Conway
A: 

good articles but can i create a resource file with html control (means not for server control)

ashutosh
@ashutosh: welcome to StackOverflow. Please read the FAQ (http://stackoverflow.com/faq). You'll see that this is a Q then click the "Ask a Question" button at the top of the page to ask your own. Do not "reply" to an existing question.
John Saunders