• 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

    • Some AMD Ryzen users can get free Windows performance boost with this simple system tweak by Sayan Sen AMD understands that there is a lot of demand for its X3D processors and for good reason too, since they offer some of the best gaming experiences. As such, the company plans to launch a new 6-core Ryzen 5 9600X3D for those who may not want to spend top dollar on a 9800X3D. What makes X3D special is the densely packed last level cache (LLC) wherein the L3 (level 3) cache is 3D die-stacked such that there is a whole lot of it that the cores can access on demand all within the smallest footprint. This is said to help with latency especially, and games happen to be quite sensitive to it since they are a mixed workload and so there is a lot of to-and-fro. However, despite that fact, users have noticed micro-stuttering and freezes on Ryzen X3D CPUs. Although there is no official fix, some of the affected users have managed to resolve the issues by tweaking a motherboard setting. The tweak is related to a setting called "GLOBAL C-STATE CONTROL" (it may be called something else by your motherboard vendor) and changing it to 'Enabled' from 'Auto' could fix stuttering and lag-related issues in games. If you are not familiar with them, Processor Power Management is done through Advanced Configuration and Power Interface (ACPI) P-states or C-states. While P-states or performance states handle CPU voltage-frequency scaling, C-states deal with CPU sleep states so that some of the CPU functions, which are not necessary at that moment, are disabled. The P-states and C-states work together to make the processor run more efficiently. It helps the OS and apps determine which cores can be parked. The Global C-state control setting helps users manage not only the DF and CPU core C-states but also the I/O C-states too. For those wondering, DF here refers to Data Fabric or AMD's high bandwidth Infinity Fabric interconnect between CPUs, GPUs, and more, on AMD systems. By default, this is set to "Auto" which also means that it is "Enabled" by default. However, in the case of X3D parts, Auto may set this setting to "Disabled" and thus manually toggling it to "Enabled" may be necessary. X3D processors, the dual CCD (core complex die) ones especially, have their V-cache on a single CCD. If the CPPC (Collaborative Processor Performance Control), which lets an OS like Windows control the "preferred core" and clock speed boost, isn't working optimally to assign the correct gaming CCD, then this fix could well work. Global C-State Auto: Global C-State Enabled: We ran a benchmark on our Ryzen 9 9950X3D to see if toggling the settings would make a difference, and well, it didn't in the case of AIDA64. However, since this is a synthetic test that measures cache and memory exclusively, we can't definitively conclude that the fix will also not make a difference in the case of games. Another remedy for stuttering is to disable the monitoring of the "Power percent" metric on MSI Afterburner if you have it on. This has been a long-known issue and in fact can help you even if you are not using an X3D CPU. Source: Reddit (link1, link2) via YouTube
    • I only have one contact on WhatsApp. And that contact has sms also. I have many more contacts that use WhatsApp also, but everyone defaults to use iMessage, SMS or RCS anyway. Not a loss for me. I'm in Norway where mostly nobody uses WhatsApp.
    • Apple is boring for a kid. Only fun is browsing websites for HTML games. A PC with steam is another story. Of course if the child plays video games all day then maybe that might not be a good idea. :-)
    • Looking for a specific setting in Settings? Sorry, the option just doesn’t exist as you’d need to elevate for that. Want to do something quickly and efficiently? Nah, forced to use a “modern” interface which takes far longer to achieve what you’re looking to do. (Example: disable a NIC)
    • Yet the best laptop for all day battery life is a Mac, hands down, no contest. Windows is bloated and power management is rubbish. Search indexer. Defender. Malicious Software Removal Tool. Windows Update (+DISM). Office CTR. Telemetry. Disclaimer: I own a surface laptop studio, multiple gaming desktops, server, and a macbook pro.
  • Recent Achievements

    • One Month Later
      DecaffKnight94 earned a badge
      One Month Later
    • Dedicated
      S.P earned a badge
      Dedicated
    • One Month Later
      adxnksd42031 earned a badge
      One Month Later
    • Rising Star
      aphanic went up a rank
      Rising Star
    • Contributor
      GravityDead went up a rank
      Contributor
  • Popular Contributors

    1. 1
      +primortal
      663
    2. 2
      ATLien_0
      261
    3. 3
      Michael Scrip
      234
    4. 4
      Steven P.
      161
    5. 5
      +FloatingFatMan
      151
  • Tell a friend

    Love Neowin? Tell a friend!