(C++ WinForms VS 2022) Icon Exception

Viewed 306

When I attempt to change the icon of the form in the properties window (this), this line of code gets generated in the .h file:

this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));

But when I try to run it, it gives me a System.Resources.MissingManfiestResourceException error:

System.Resources.MissingManifestResourceException HResult=0x80131532 Message=Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Thermodynamics_Generator.Form1.resources" was correctly embedded or linked into assembly "thermodynamics_generator" at compile time, or that all the satellite assemblies required are loadable and fully signed. Source=mscorlib StackTrace: at System.Resources.ManifestBasedResourceGroveler.HandleResourceStreamMissing(String fileName) at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark) at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark) at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents) at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream) at System.Resources.ResourceManager.GetObject(String name) at Thermodynamics_Generator.Form1.InitializeComponent() in C:\Users\my_name\source\repos\thermodynamics_generator\thermodynamics_generator\thermodynamics_generator.h:line 1194 at Thermodynamics_Generator.Form1..ctor() in C:\Users\my_name\source\repos\thermodynamics_generator\thermodynamics_generator\thermodynamics_generator.h:line 26 at main() in C:\Users\my_name\source\repos\thermodynamics_generator\thermodynamics_generator\CppCLR_WinformsProject.cpp:line 11

And I have tried something like this from another post to do this instead:

this->Icon = gcnew System::Drawing::Icon(L"app.ico");

It works, but when ran on other computers, it just does not want to run at all (only wants to run on my computer).

I also checked the .resx file to see $this.Icon is there but still gives me an exception.

If there is other things I mention, let me know.

1 Answers
this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));

This line of code is automatically generated, where $this.Icon contains the file path, you could read the documentation for the GetObject(). And the documentation states that

If the appropriate resources cannot be found, the constructor call creates a valid ResourceManager object. However, the attempt to retrieve a resource throws a MissingManifestResourceException exception. For information about dealing with the exception, see the Handling MissingManifestResourceException and MissingSatelliteAssembly Exceptions section later in this article.

We could conclude that the program did not find your resource file. If you want to run the program on other computers, you must ensure that the icon path is consistent with your computer.

Edit:

I suggest you to check if the resource is included as a project, select All Files in Solution Exploer, then right click on the resource file and select Include In Project.

enter image description here

enter image description here

Related