• 0

Loading Icon from resources


Question

I added a minimize to tray function, to do that i need to assign an icon for the tray, the problem is the only way i can do that is this

 Icon ico = new Icon( "calculator.ico");
appIcon.Icon = ico;[/CODE]

witch means that calculator.ico should be in the same folder as the program. Now i have added calculator.ico to the program resources but i dont know to assign it to the variable ico, could someone help me plz?

Link to comment
https://www.neowin.net/forum/topic/869632-loading-icon-from-resources/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

All your resources you put in Resources.resx are accessible through:

Icon MyIcon = global::MyNameSpace.Properties.Resources.MyResourceName;[/CODE]

where MyNameSpace is your default namespace for the program and MyResourceName is the icon resource you're looking for. Note that this works for all types or resources, simply change the variable type to String or Image or whatever depending on the resource type. The global:: prefix is not required but recommended.

If you want to get a resource with a dynamically retrieved resource name, use the ResourceManager:

[CODE]string resName = "MainIcon";
Icon MyIcon = (Icon)global::MyNameSpace.Properties.Resources.ResourceManager.GetObject(resName);[/CODE]

  • 0

Hey Calculator, thanks for the reply i tried the method you mentioned so the code now is as follows

      string resName = "calculator.ico";
            Icon MyIcon = (Icon)global::Calculate.Properties.Resources.ResourceManager.GetObject("calculator.ico");

            appIcon.Icon = MyIcon;

When i ran it, it didnt give any building errors but i tried to minimize to tray the form simply disappeared but it was still running in the process, i setup a break point at the last sentence and ran it again, and by the time i got to that sentence i checked the MyIcon variable and it was still Null, so im guessing the problem is that i didnt the icon to the resources correctly. Dont you add an icon from the add new component tab? cause thats what i did, where did i go wrong?

  • 0

When you add an icon through the Add Component window, you're only adding it to your project. The compiler won't do anything with it unless you add it to your resources.

  1. Copy the icon you want to use somewhere in your project folder.
  2. Make sure it's included in your project.
    If it doesn't appear in the Solution Explorer, click the "Show All Files" button in the Solution Explorer toolbar, find the file in the tree, right-click and choose "Include in Project".
  3. Navigate in the Solution Explorer to Properties > Resources.resx and open it. You should now have Resources.resx opened in the editor.
  4. In the top toolbar underneath the file tabs, open the dropdown menu saying "Strings" and change it to "Icons" - or just press Ctrl+3.
  5. Drag the icon from the Solution Explorer to the editor. This should add the icon to the list of icon resources in Resources.resx.
  6. Optionally, rename the resource.

And now, you can use the icon resource in your project! :)

string resName = "calculator";
Icon MyIcon = (Icon)global::Calculate.Properties.Resources.ResourceManager.GetObject(resName);

appIcon.Icon = MyIcon;

However, if you can, try to use:

Icon MyIcon = (Icon)global::Calculate.Properties.Resources.ResourceManager.calculator;

Note: the resource names follow the same naming restrictions as C# variables. This means you can't have a file extension in the name of the resource since the dot is not supported. Since the ResourceManager holds all resources of all types, you can't have two resources with the same name even when they're of different types.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.