Help getting the straight on .resources/Icon(Type, string) constructor

  • Thread starter Kendall Gifford
  • Start date
K

Kendall Gifford

Greetings.

While trying to get a simple app working, I've been forced to delve into
embedded and/or linked resources a bit. I read all the reference for the
System.Resources namespace as well as all the material within the
"Resources and Localization..." tutorial. While I'm confident I now know
completely how to accomplish my original, simple task of embedding some
icons into my assembly and use of the same at runtime, I realize I've a
few unanswered questions on this topic.

Somehow, the docs for the System.Drawing.Icon class convinced me that I
should be able to very quickly extract an icon from a resource within my
assembly by merely using one of the classes overloaded constructors.
Specifically, the constructor taking two arguments: Icon(Type type,
string resource). According to the docs, this creates an instance from a
resource with the name specified by the string argument that is
contained within the assembly that contains the type specified by the
Type parameter.

After playing around a bit with how I *thought* this was supposed to
work I realize I just have no idea what I'm doing here. So, can anyone
give me some illumination or example as to how this Icon constructor is
intended to be used?

Thanks to anyone who chooses to humor me.
--
Kendall Gifford
========================================
WEB: http://kendall.jedis.com/
EMAIL: [REPLY TO NEWSGROUP PLEASE]
========================================
 
J

Jon

When pass the Type, you are saying 'use the same assembly and namespace as this Type'.
It does not matter what kind of Type it is.
 
K

Kendall Gifford

Jon said:
When pass the Type, you are saying 'use the same assembly and namespace as this Type'.
It does not matter what kind of Type it is.

From the caught exceptions, I see that it is indeed looking for my
resource in the namespace as the type argument:

"Resource 'AppIcon16x16_32' could not be found in class
'ZMA.Base.CExitForm'."

I guess I should be specific about what I was trying so you'll know
which of my misconceptions need fixed. I have a class inheriting from
Form that contains the following in InitializeComponent():

public class CExitForm : Form {
...
private void InitializeComponent() {
...
this.Icon = new System.Drawing.Icon(
this.GetType(),
"AppIcon16x16_32"
);
...
}
...
}

AppIcon16x16_32 is the name I gave one of my Icons as I put into a
resource file "Images.resources". Finally, I build the test app using
the following on the command line:
csc.exe /target:winexe /out:ZMA.exe
/res:Images.resources,ZMA.Base.Images Apptest.cs
(my csc.rsp provides all the /r[eferences])

When I run it, I get the System.ArgumentException from my call to the
Icon() constructor with the message described above.
**********

So, other than this I've tried various combinations naming my resource
(/res:Images.resources,[various combos]), I've tried linking to the
resource file instead of embedding it (/linkres:...) though I pretty
much knew this wasn't related to the problem, and I've tried different
values for the string argument of the Icon constructor.

Either I just keep missing the correct combination, or - more likely - I
am barking up the wrong tree. Thanks again for all help rendered.

--
Kendall Gifford
========================================
WEB: http://kendall.jedis.com/
EMAIL: [REPLY TO NEWSGROUP PLEASE]
========================================
 
K

Kendall Gifford

Jon said:
Run ILDASM.
Check to see if 'AppIcon16x16_32' is indeed in the 'ZMA.Base' namespace.
It isn't. AppIcon16x16_32 isn't anywhere to be seen via ILDASM. The only
thing that ever shows up is either:
csc.exe ... /linkres:Images.resources,ZMA.Base.Images
..mresource public ZMA.Base.Images
{
.file Images.resources at 0x00000000
}

.... OR ...
csc.exe ... /res:Images.resources,ZMA.Base.Images
..mresource public ZMA.Base.Images
{
}

But even when I replace 'AppIcon16x16_32' with various strings like
'Images' or 'ZMA.Base.Images' or 'Images.resources' or even
'Images.AppIcon16x16_32' and other combinations, it doesn't seem to
work. Does anyone have a complete working example of using this constructor?

--
Kendall Gifford
========================================
WEB: http://kendall.jedis.com/
EMAIL: [REPLY TO NEWSGROUP PLEASE]
========================================
 

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