By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

Reusing an 'existing' form in a new windows form project?

P: n/a
Dear Professionals,

I have recently been using the wonderful krypton toolkit and am trying
to use them in my small hobby application. I include this bit of info
as an aside really because i'm sure my question can be extrapolated to
the more general case, so here goes!

I have a box standard windows forms project. (File, New Project,
Windows Application, OK)

I have added a form2 to this project using the Add New Item button, and
then choosing Windows Form. So far so simple.

However I want to use a form from an existing project. I have two
versions of Visual Studio lets say A and B.

A - has the solution open that has a form in that I want to use.
B - has my live working project in it. I would like the form from
version A in B.

First I tried right clicking the form in the solution explorer of A,
and then pasting it into the solution explorer of B. That didn't work
however i got the message that 'The source file for this operation
cannot be found in this solution'.

So next I tried creating a new form in B. using Add new Item, and
choosing windows form. I copied the contents of the forms code in
question from solution A, and pasted this over the top of the newly
created forms code in version B, so that I had entirely replaced the
wizard generated code in version B with the form I actually wanted from
version A. I noticed that the form appearance now looked right, but
that none of the controls appeared on the form. So I went back to the
original form on version A that I was trying to replicate, and used
Edit Select All. I then went back into my half created form in version
B, and pasted these controls. Now my form in version B looked identical
to the form in version A. So I had thought I had triumphed!

If you are still with me thankyou very much, this isn't easy to explain
in words, so i'm sure it's even harder to understand!

However I have a problem in that I can not display my newly created
form!

At the moment my program.cs in version B (the version of Visual Studio
that has my live project i'm working with) has the following as it's
main method: -

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Form2 frm = new Form2();
if (frm.ShowDialog() == DialogResult.OK)
{
Application.Run(new Form2(frm.UserName));
}

}

The new form i've created using the above processes is called 'form3'
however if i change Form2 above to read Form3 and run the programme i'm
told that the 'type of namespace Form3' could not be found. I have
tried changing the case so that it reads form3, but this also doesn't
work. I have tried saving the solution, (CTRL SHIFT S = save all) and
it still doesn't work. Yet when I view solution explorer my form3 is
sitting pretty and smiling back at me, I can see it and it's listed as
form3 in the solution explorer.

Any ideas please why this isn't displaying would be greatly, greatly
appreciated!

Please excuse the diatribe above but I have re read and re read it, and
despite the fact that my need is fairly simple - (wanting to use a form
from another project in a new project) I couldn't find a terser way of
putting it!

Thankyou,

Gary.

Jan 3 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<ga********@myway.comwrote in message
news:11*********************@a3g2000cwd.googlegrou ps.com...
[...]
Please excuse the diatribe above but I have re read and re read it, and
despite the fact that my need is fairly simple - (wanting to use a form
from another project in a new project) I couldn't find a terser way of
putting it!
Even with your lengthy description, it's not clear to me what went wrong in
your attempt to copy and paste the form from one project to another. In
theory there should be no reason that wouldn't work, but of course you need
to make sure that you copy *everything*, and make sure that all the events
that were hooked up in the original form are hooked up properly in the
copied form.

The form is simply the designer part and the code part. The designer part
describes the visual layout of the form along with initial properties for
the form and contained controls, and the code is of course the code you
wrote to support the form. As long as you've completely copied both
correctly, there should be no problem.

That said, if you're reusing the form, and especially if you expect to reuse
it more than once, you may find that it makes more sense to simply keep the
form in its own project, and then reference that project by any other
project that wants to use the form. Of course, if you do this you'll
probably want to use more descriptive names than "Form2" and "Form3". But
the general technique is better for code reuse than simply copying and
pasting existing code.

Pete
Jan 3 '07 #2

P: n/a
Thankyou Pete, i'll try your method in future versions.

For anyone reading this with a similiar problem, the error was that I
was referencing the form as form3 in the program.cs main method,
(because this is how it was listed in solution explorer.)

However the form should have been referenced using projectname.form ,
despite the fact that I had copied it into a new project.

Gary-

Peter Duniho wrote:
<ga********@myway.comwrote in message
news:11*********************@a3g2000cwd.googlegrou ps.com...
[...]
Please excuse the diatribe above but I have re read and re read it, and
despite the fact that my need is fairly simple - (wanting to use a form
from another project in a new project) I couldn't find a terser way of
putting it!

Even with your lengthy description, it's not clear to me what went wrong in
your attempt to copy and paste the form from one project to another. In
theory there should be no reason that wouldn't work, but of course you need
to make sure that you copy *everything*, and make sure that all the events
that were hooked up in the original form are hooked up properly in the
copied form.

The form is simply the designer part and the code part. The designer part
describes the visual layout of the form along with initial properties for
the form and contained controls, and the code is of course the code you
wrote to support the form. As long as you've completely copied both
correctly, there should be no problem.

That said, if you're reusing the form, and especially if you expect to reuse
it more than once, you may find that it makes more sense to simply keep the
form in its own project, and then reference that project by any other
project that wants to use the form. Of course, if you do this you'll
probably want to use more descriptive names than "Form2" and "Form3". But
the general technique is better for code reuse than simply copying and
pasting existing code.

Pete
Jan 4 '07 #3

P: n/a
<ga********@myway.comwrote in message
news:11*********************@q40g2000cwq.googlegro ups.com...
[...]
However the form should have been referenced using projectname.form ,
despite the fact that I had copied it into a new project.
For what it's worth, this suggests that you didn't really copy it correctly.
That is, you copied more than just the code that supports the form. You
copied the namespace declaration as well, putting the form into a different
namespace than the one the target project is using.

So, rather than changing your references to form to "projectname.form", you
could have simply fixed the namespace declaration for the copied form to
match your project, and used the form normally.

Copy and paste is fraught with little gotchas like this. And of course, now
you have two copies of the exact same code, so when you find a bug in the
code, you'll have to remember to fix it in both places. Just a couple of
the reasons why copy and paste is bad, reuse by reference is good. :)

Pete
Jan 4 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.