Groups | Blog | Home
all groups > c# > september 2007 >

c# : 'embedded resources' and Dispose error..?


Marc Gravell
9/8/2007 2:01:22 PM
*what* are you setting as "embedded resource"? Form1.cs? What are you
trying to do? Basically, don't change this. You /do/ want to compile
this file, so leave it as "Compile" Embedded resource means that the
file is dumped (verbatim) into the assembly as a resource, useful for
including support files (xml, graphics, etc) with the app without
needing to deploy these files separately. But this is not how you
treat code, unless you genuinely intend to bundle the source file with
the app (instead of compiling it). Which would be rare.

By defualt, forms in VS (and express) use "partial classes"; this
means that there is a second file (Form1.Designer.cs) that is part of
the same class. But by changing the build action, the only file it can
find (for the Form1 class) is Form1.Designer.cs - which doesn't
explicitely know that it is a Form (this is part of Form1.cs), and
hence has no Dispose(bool) method to override.

Marc

Fred*
9/8/2007 8:58:56 PM
Hello,

I'm using Visual C# 2005 Express.
if I create a new "application windows" project and run it (F5), it works
well. (an empty window is launched..)

as soon as I set the build action to embedded resource, I can't run it
anymore because I got an error on
'WindowsApplication1.Form1.Dispose(bool)'..

I find several examples online about embedded resource, but nothing that
give me an explanation on what I should do about that.
I'm beginning with C#, so I surely missed something, but I already search
for a moment without finding a solution, so if someone could help me a
little, it would be nice.

thanks.
Fred


Fred*
9/8/2007 11:56:59 PM
OK, thanks.. but of course my example was just to give you the most simple
way to reproduce my problem (in case it was needed..).
but of course my real program is bigger than that and yes I have support
files (graphics) that I want to include.

so your answer seems to explain me why the examples I found online didn't
have this problem, because they was made with VS 2003 and there wasn't an
external Form1.designer.cs. that was simple example in only one file.

well, now, how can I make it go with "partial classes" if I set the build
action to embedded resource..?

thanks.
Fred



Fred*
9/9/2007 12:06:27 AM
sorry, English is not my mother language and I think I replied a little too
fast, without reading what I wrote.. should have been clearer.
thanks again.
Fred

Marc Gravell
9/9/2007 12:43:21 AM
[quoted text, click to view]

Please can you explain *why* you want to set it as an embedded
resource? Basically, I'm pretty sure that this isn't going to do what
you think it is... so tell us what actually want to do, and we might
be able to suggest the right way to achieve it.

Marc
Marc Gravell
9/9/2007 12:47:00 PM
It is perfectly possible to add images; first, however, you need to
get the images into the project. Using Visual Studio (including
"express") you can drag files from Explorer into the solution explorer
(the tree with the files). Once the images are in the solution, there
are 2 ways to get access to them (in both cases, leave the build
action of the form as "compile")

The easiest option is to set the build action of the image(s) to
"content" (the default), and add a resource file (there may already by
one called Resources.resx) in the Properties folder. Open the resource
file (View Designer), and drag the image from the solution explorer
into the resource designer (anywhere... it doesn't matter). This tells
the compiler to embed the image into the resource file, which itself
will be embedded into the exe. You can now access the image by name,
simply by looking at ResourceFileName.ImageFileName - i.e. for me my
resource file is "Resources.resx", and my image is "Test.jpg", so I
can use Resources.Test (which is an Image). Using the same example, if
it doesn't compile (The name 'Resources' does not exist in the current
context) then right-click on the Resources (underlined in blue in the
editor), Resolve -> using [some namespace]. It should then work. Your
images should also now appear automatically when selecting button
images etc.

The second option is to change the build action of the image(s) to
"embedded resource"; however, it is trickier to get hold of these -
you need to use Assembly.GetManifestResourceStream({name}), and
process the stream manually.
Fred*
9/9/2007 6:39:19 PM
Hello,
what I want to do is having only one file, with my different small images
embedded within and be able to access thoses images for my software.

since I see different examples doing that, like those two :
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75
http://support.microsoft.com/kb/319292/en-us

I tried to make something similar, but there wasn't "partial classes" in
those examples.
but as you said, maybe there's now another way to do that..

Thanks again.
Fred

Fred*
9/9/2007 8:17:30 PM
Wow, I'm sorry, I think I got it.. I'm not near my PC with my C# project,
but it seems so evident to me just now, that it's a pity I didn't think of
it yesterday.
well I set the "embedded resource" build action to my Form1.cs, but I just
need to set this to the graphic files I need to embed, not to my Form..

many thanks again and sorry to eat your time for a so stupid error of mine..
Fred.

Fred*
9/9/2007 11:34:58 PM
yes, with the resx file is really easy for what I need yet..
big thanks.
Fred

AddThis Social Bookmark Button