valhalla
valhalla

Reputation: 73

Added existing form to a project but it is not recognized or useable

I am able to add an existing form to a project and see the code and the visual form itself. BUT the C# project that I added this form to does not recognize the form. I tried to make it the form that opens when you run the project by putting the form name in Program.CS but it gets an error because it does not recognize the form name and gripes asking about a reference or dependency possibly missing. I used to do this in VB all the time without any problems. So I tried to find what kind of reference or dependency I need to add and where to put it etc. etc. but I found nothing.

There is no code to copy here really because it is so basic and general. I created a new project and deleted the projects default form1. Then I added the existing form from any other simple project using add -> existing item to the project. I also tried copying the .cs, .resx and the Designer.cs form files outside of visual studio and then adding the existing file.

"Using System.Windows.Forms" and "using System.Drawing" are in the project. Obviously I'm missing something and MS has made this more complicated and encryptic than it should be. I'm assuming they must be handling "outside" forms as some independent class or something. Will someone please help me with this??? It cannot be that difficult. Can you just make up an example with some form name from another project and some new project name and show everything that has to be done to make this just like any other form that you might create in this new project??? ...In other words so it can be used.

Thank you!

VH

Thank you for all the response! So I guess there is no way to make the added form take the place of the original form???? You know not showing up as a class type (green) but as if it were some kind of template form added and making it the original form. I just wanted to be able to use an existing form as a reusable form so I don't have to remake the same form with all the buttons etc. You know like the purpose of a class.... :)

Upvotes: 0

Views: 8065

Answers (4)

gaurab15
gaurab15

Reputation: 1

As suggested by Ewalldinho, adding only the Form1.cs with the Project > Add Existing Item menu works best. You might have to refresh your Solution Explorer for it to import all dependent files.

Also, do not forget to make any required namespace changes in both Form1.cs and Form1.Designer.cs

Upvotes: 0

Ewalldinho
Ewalldinho

Reputation: 56

I don't know if I understood your problem correctly, but since answers about namespaces didn't solve your problem, my best guess would be this:

When you did Add -> Existing Item... and chose form files, Visual Studio added those 3 files (Form1.cs, Form1.Designer.cs and Form1.resx) as separate items, rather than Form1.cs with related designer and resource files nested together. And when you open Form1.cs in designer mode, you see only empty form (without any control). That's how I understand your lines:

BUT the C# project that I added this form to does not recognize the form.

If this is the case, such problem can be solved by editing project file * .csproj. If you compare the files of original project and a new project with addded existing form item files, you'd notice that original project has these lines:

<Compile Include="Form1.cs">
  <SubType>Form</SubType>
</Compile>

<Compile Include="Form1.Designer.cs">
  <DependentUpon>Form1.cs</DependentUpon>
</Compile>

<EmbeddedResource Include="Form1.resx">
  <DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>

While the new project file would have these lines:

<Compile Include="Form1.cs">
  <SubType>Form</SubType>
</Compile>

<Compile Include="Form1.Designer.cs" />

<EmbeddedResource Include="Form1.resx">
  <SubType>Designer</SubType>
</EmbeddedResource>

So you need to open * .csproj with editor and mark Form1.Designer.cs and Form1.resx files as "DependentUpon" Form1.cs file.

This is not a nice solution (you need to edit project XML file instead of some nice click in Visual Studio IDE)... So to avoid such situation, I recomend: when adding existing form item to project do not select all 3 files, but only the main form file (in this case "Form1.cs"), the rest Visual Studio adds itself.

Upvotes: 3

valhalla
valhalla

Reputation: 73

I did a test by creating 2 new projects. One called "TestHasFormToAdd" and the other "TestAddingForm". I carefully named the original forms in each project "FormToAdd" and "OrigForm" respectively. I added the existing form, "FormToAdd" from "TestHasFormToAdd" by adding the FormToAdd.CS file. Then I opened the Program.cs file and added the following: using TestHasFormToAdd; This was the namespace under the added forms code. Then I was able to change the line for opening the form from:

Application.Run(new OrigForm()); to

Application.Run(new FormToAdd());

It just appeared in green.Then I deleted the line: using TestHasFormToAdd; and changed the Application.Run line to:

Application.Run(new TestHasFormToAdd.FormToAdd());

Both worked as I ran the program and my added form came up and worked.

Upvotes: 0

competent_tech
competent_tech

Reputation: 44941

The most likely cause of your problem is the namespace of the existing form, which will almost certainly be different than the namespace of the code you are attempting to open the form from.

Providing the full namespace for the form, like MyProject1.Namespace.Form1, should resolve the problem.

VB projects can have a slightly different implementation: a default namespace is specified in the project settings, so many developers don't qualify all the classes (including forms) in VB projects with an additional namespace.

Upvotes: 0

Related Questions