Why so hard to load icon from embeded resource?

E

Ed Sutton

What's the trick to loading an icon from the embedded resources?

Here are the steps I have taken:
-------------------------
1 - I added a file named "FOLDER01.ICO" to my solution project
2 - I changed "Build Action" for the file to "Embedded Resource"
3 - At the end of my constructor, I try to load an icon and it throws the
following exception:

Exception:
"Resource 'myProject.frmMain.FOLDER01.ICO' could not be found in class
'myProject.frmMain'."


namespace myProject
{
public class frmMain : System.Windows.Forms.Form
{
public frmMain()
{
// This throws an exception
Icon = new Icon(typeof(frmMain),
"myProject.frmMain.FOLDER01.ICO");
}

Thanks in advance for any tips or suggestions,

-Ed
 
E

Ed Sutton

Never mind. I found a syntax that works. These two lines will work:

Icon = new Icon(typeof(frmMain), "FOLDER01.ICO");
Icon = new Icon(GetType(), "FOLDER01.ICO");

When I tried to match the syntax in the Petzold book, "Programming Windows
with C#", for some reason, the following throws the exception I mentioned.

Icon = new Icon(typeof(frmMain), "frmMain.FOLDER01.ICO");

Oh well, move on.

-Ed
 
E

Ed Sutton

And another problem I was having is that the resource name is * case
sensitive *. It must match the case sensitivity of the embedded resource
file name you added to your solution.

For example, if you added a file named "FOLDER01.ICO" to your solution and
made it an embedded resource. The following works:

Icon = new Icon(typeof(frmMain), "FOLDER01.ICO");

This will fail:
Icon = new Icon(typeof(frmMain), "folder01.ico");

This somehow suprised me. I think this is the first time, using a Windows
programming language, that I have run into file name case sensitivity. I
suppose that once it is embedded as a resource, it is no longer just a file
name.

-Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top