'embedded resources' and Dispose error..?

F

Fred*

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
 
M

Marc Gravell

*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
 
F

Fred*

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
 
F

Fred*

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
 
M

Marc Gravell

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

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
 
F

Fred*

F

Fred*

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.
 
M

Marc Gravell

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.
 

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