How to restore a Winform in design mode - c#

Today something bad happened with my Project and I lost my form (If I view Design) but if I start the program the form is Ok, with all items that I inserted before.
I've realized that Form1.cs is not with Form1.Designer.cs
How can I restore the form in design mode?

Open AudioPlayer.csproj in a TextExitor like notepad++ and find Form1.Designer.cs then you can add DependentUpon node:
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
Based on your last comment there are two InitializeComponent methods, please verify both and remove the one of them.

Related

Missing design file

I was trying to save my project, but by mistake I save it on desktop and have only .cs file and Designer.cs. Is there any solution to restore [design] file and my whole project?
designer.cs is in fact the file you are looking for.
The problem is due to manually adding it as part of a project. Creating the form from Visual Studio actually does some extra tricks with the project file.
What you want to do is open your csproj in a text editor and correct it like so.
<Compile Include="stats.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="stats.Designer.cs">
<DependentUpon>stats.cs</DependentUpon>
</Compile>
That should do it. Notice the SubType actually specifies that it is a Form. This is what Visual Studio looks at to figure out what kind of designer or view to display. DependentUpon is what allows files to nest underneath each other.
If this doesn't work for some reason, you can also create a new form and copy the contents of the designer.cs class into your new one and all of your controls should show back up. This can also help you fix your resx file. It's probably best to go this route at some point anyway so you can better understand how a csproj file is structured in general. There's a good chance, this won't be the only time you need to look at the XML.

Windows form is associated with the wrong code

When clicking 'View Code' on the form in Visual Studio 2013 I am shown only the automatically generated design code. Similarly, if I try to add a new event handler via the events section of a control's 'Properties' pane, then the code is automatically added to the designer code which is obviously not correct. Trying to add the event to the actual form code is fine, but it will not let me associate it with a control on the form. However, all previous event handlers remain active and correctly associated with the form and it's controls.
Strangely enough, there is another blank form present in the solution explorer that, when clicked, takes me to the actual form code mentioned above.
I suspect that this issue was caused when I copied the form code into the solution from another (the old solution messed up because stupid me didn't use version control) hence my association issues. If someone could explain how I can go about regaining the correct associations between the files again it would be much appreciated.
Extra information - the solution hierarchy shows two forms, one blank and one actual form. The real form code file is shown below both.
The first thing I would check is the project file; I suspect you've got some bad mapping between designer/code files in there. You'll need to right-click on the project and select Unload Project, then right-click it again and select Edit XXX.csproj (You can also open up that file in any other program, it's just an XML file). Look for the <ItemGroup> node that contains the <Compile> nodes; on a brand new winforms app it looks like this for the generated form:
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
...
Make sure that your designer files are dependent on the correct code files.

Reattached Designer.cs to mainform.cs

So when I grabbed some files of a different solution, and pasted them into a different solution, someone my Mainform and designer got detached. If I run my program the form shows correctly, but when i view my form in design mode, its a blank form. Anyone know how to reattach the designer?
Thanks for your help.
Edit the .csproj file with a text editor (Notepad is fine) to add the missing <DependentUpon> element. So it looks like this:
<Compile Include="Yadayada.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Yadayada.Designer.cs">
<DependentUpon>Yadayada.cs</DependentUpon>
</Compile>

Not able to open a form in design mode

When i open form, i can't get into design mode and i get this error:
The variable 'MonthViewCalendar'
(internal
Infragistics.Win.UltraWinSchedule.UltraMonthViewSingle
MonthViewCalendar;)
is either undeclared or was never assigned.
Any idea why a form doesn't go into design mode? I am using VS 2010.
I tried - Clean solution, restarting VS and reopening, but that didn't solve my issue.
Sometimes you may find that the project file loses the correct subtype that the file needs to open in design mode.
To fix:
Shutdown VS, then edit the projects ".csproj" file using a text editor
Look for the <Compile Include="MyFile.cs">
If there's no "<SubType>Form</SubType>" then add it back in as follows
<Compile Include="MyForm.cs">
<SubType>Form</SubType>
</Compile>
Go into your Form.Designer.cs file and remove the declaration for "abcd". Then reload it in your designer.
It can happen if you insert a new class above an implementation of the class.
What works for me is to close and relaunch Visual Studio, then rebuild. After that the form can be opened in design mode. I wouldn't say I particularly like my solution, but I haven't come up with a more sure-fire solution.
Edit: I've only ever encountered this problem when there's an Infragistics control on the form.

Form showing as a class in Solution Explorer

I have a form in my project that is showing up as a class in Solution Explorer. This is causing a problem since I can't get to the designer. Any ideas on how to fix this?
You can fix the problem manually by editing your csproj file.
Open it in notepad and search for the filename of the class. You should see something like this...
<Compile Include="frmTest.cs" />
Add a subtype called 'Form' like this...
<Compile Include="frmTest.cs">
<SubType>Form</SubType>
</Compile>
Reload the project. Visual Studio should now identify the file correctly.
Try seeing if there are any hidden files by using the the 'Show All Files' button. Sounds like your project file might not have all of the files added to it.
One thing that has worked for me in the past is to just add another blank form to the project and that seemed to make the other form show up as a form. You can then delete the blank form.
I don't know if it will work for you, but it only takes a few moments to try it.
Chris
remove the reference of form related dll and again add and build the project
Press F7 when in code editor to get to the designer. And restarting Visual Studio usually helps.
Simply select the form and press Shift+F7. You'll find your form.

Categories