• 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.
  • Posts

    • How to enable the redesigned Windows 11 Start menu by Taras Buria This week, Microsoft started testing a big redesign for Windows 11's Start menu. It is now rolling out to Windows Insiders in the Dev and Beta Channels, addressing quite a few pain points in the original Start menu, which was shipped in late 2021. Nearly four years later, we finally have the long-anticipated redesign (announced earlier this year). The updated menu now features a single-page view, with all your pins and apps on one scrollable page. You can change the list of all apps between three variants: list, grid, and category. More importantly, you can now hide the recommended section in the Start menu settings (one of the most requested Start menu-related changes). The new Start menu is available in this week's Dev and Beta update. However, like always, Microsoft is rolling out changes gradually, which means you might not have the lucky ticket even on the latest build. If you do not want to wait, you can force-enable the new Start menu and its Phone Link button using a few commands in the ViVeTool app. Here is how to do that: Download ViveTool from GitHub and unpack the files in a convenient and easy-to-find folder. Run Command Prompt as Administrator and navigate to the folder containing the ViveTool files with the CD command. For example, if you have placed ViveTool in C:\Vive, type CD C:\Vive. Type vivetool /enable /id:47205210,49221331,49381526,49402389,49820095,55495322,48433719 and press Enter. Restart your computer. As usual, keep in mind that stuff in preview builds is less stable, so be aware of the risks of running preview builds. While Microsoft is not saying when the new Start menu will be available to all users, it will probably not take too long before it shows up in Release Preview and non-security updates. By the way, if you are curious, check out some of the prototypes that Microsoft considered when designing the new Start menu. Credit for the IDs goes to @phantomofearth on X.
    • reddit's "bottom of the barrel" > 99% of neowin front page "news"
    • Mozilla has decided to pander to certain specific crowd. I was a die hard firefox fan until the latest CEO decisions came to affect the browser quality. Had to move to another, sadly, chromium based browser.
    • I don't know if its just me,but I found the "frosted" glass effect to be much better looking in macOS than iOS and iPadOS
    • The Start menu is a defining UI element that made Windows, well, Windows. naturally people will be worked up to any changes it receives.
  • Recent Achievements

    • Week One Done
      IAMFLUXX earned a badge
      Week One Done
    • One Month Later
      Æhund earned a badge
      One Month Later
    • One Month Later
      CoolRaoul earned a badge
      One Month Later
    • First Post
      Kurotama earned a badge
      First Post
    • Collaborator
      Carltonbar earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      ATLien_0
      268
    3. 3
      +FloatingFatMan
      237
    4. 4
      +Edouard
      201
    5. 5
      snowy owl
      162
  • Tell a friend

    Love Neowin? Tell a friend!