I'm working on a winform app.
The design view worked well until I renamed the Form1.cs to FormMain.cs.
The program itself is ok. It can be compiled with no error and it runs well(with right form). But visual studio keep warning me that there are mistakes in FormMain.cs. It seems that FormMain class in FormMain.Designer.cs is totally ignore as it alerts "InitializeComponent(); is not defined."
And the design view goes blank as if the form was just newly-created.
IntelliSense tells me the origin(not yet renamed) Designer.cs is not found.
Now if I try to create anything on the blank form, the code is directly written in FormMain.cs.
How can I solve this?
Thanks.
Using Visual Studio 2019, I was following the C# WPF tutorial from Microsoft docs. Along the way, there is a step wherein you rename MainWindow.xaml to Greetings.xaml. When I did this, the design view was gone. Restarting Visual Studio fixes the design view. Of course, save your files before restarting.
Removing the form and adding it again to the solution fixes this issue. Here's how you can do:
Right click on the FormMain in the solution explorer and "Exclude From Project". Now right-click on the solution and click Add -> Existing Item and select the FormMain.cs from the same solution folder.
I have been working on c# Gui for about a week without problem, all of a sudden when I added a label control I tried to access its events and saw the error "This document item has no code behind file. Add code behind file and a class definition before adding event handlers"
This is odd since the gui/methods still runs fine but it is making it very difficult to progress...any ideas? Did a file get deleted etc? Can provide more info on request, thanks for the help!
(couldn't add screen shot unfortunately yet but error is displayed in properties window/event tab)
I encountered the same error"This document item has no code behind file. Add code behind file and a class definition before adding event handlers" in my first C++ Helloworld project when I try to add a even handler for one button I added in the MainPage.xaml.
I solved the problem by restart Visual Studio 2015 and recreate a new project, then I add a even handler for the button successfully.
It's my experience and just be posted here that maybe you can use it as reference.
I ran into this, too. In my project, most UserControls were fine, but there was one group that showed the "no code-behind" message instead of properties. The .cs files did exist, though. In the Solution Explorer tree, the xaml files all had xaml.cs files. I checked the csproj file, and all the xaml.cs files were tagged as <DependentUpon> the correct matching xaml file.
I was able to fix this by closing the solution, then deleting the csproj.vspscc (Source Control Project Metadata) file for the project. When I reopened the solution, that metadata file was rebuilt and the Properties were available in Visual Studio again.
Has anyone had this issue:
I have a class for which code autocompletion works (it can see its members, properties, etc.), I can hit F12 and see the class code. But, the class cannot be seen in the solution explorer, nor can I add it because Xamarin Studio claims it has already been added to the solution.
Switching tabs in the notebook usually highlights the classes in the solution explorer, but for this single class, it doesn't work.
Any ideas ?
Thanks.
Your class already exists in the file system. In VisualStudio you can choose to show hidden files in solution explorer and include it back in to the project. In Xamarin Studio you could right click on solution and choose Open Containing Folder, find the file on the disk and delete it, so you can add it again.
Having the same project opened in both, Visual Studio and Blend, I use the former to write code while I edit the XAML in the latter.
When I add an UI element to the .xaml file and save it in Blend, if I have that file opened in Visual Studio too, I see the message that some external changes have been done and asking if I want them to take effect in the opened file*. I accept it, and I can see the changes take effect in the .xaml file in Visual Studio.
The problem is, that although the new elements have been added to the .xaml file, I'm not able to access them throug Intellisense in the code-behind .cs file.
For example if I add following element to the MainPage.xaml using Blend:
<Button x:Name="MyButton" Content="Button"/>
I can see that same line added to the page in Visual Studio, but I cannot access it using the name MyButton in MainPage.xaml.cs file. Intellisense is not working for it. If I write, for example MyButton.Content = "Hello!"; it is red underlined, however I can build and run the project.
Rebuilding the project doesn't do the trick, and Intellisense doesn't work until I save the file "again" in Visual Studio.
I feel this behavior annoying sometimes, and I'd like to know if it is the normal behavior, or there is something wrong in my configuration.
*I usually don't have it opened in Visual Studio, just to avoid that meessage being shown for each modification in Blend
I have just check your problem actually it is not a issue. everything is fine so what you have to do is save your xaml page on which you have added the button after accepting the changes that was done in blend..(do control + s on your xaml page one more time it will work fine)..sorry for my bad english..
I encountered this issue when developing Xamarin Forms in Visual Studio 2015 and somehow the problem went away after I:
1) Unload project (right-click project and select from context menu)
2) Edit .csproj (from context menu)
3) Pretend to modify and save csproj. :)
4) Reload project (from context menu)
Closing the whole solution and re opening it fix my similar issue.
I am in process of writing a C# Windows Forms application using Visual Studio Express 2010 ENU SP1. Further VS specifics are at the bottom of this post. I recently made some changes using the designer view to the layout and then decided that I did not want the changes so I closed the designer window. The code window was still up when I closed the project and the changes were saved any way. I don't care about the changes because the layout can be restored easily except for the fact that I cannot find any way to reopen the visual designer window. To get the window back I have tried the following:
Shift F7
Right click in the designer code window
Double click the designer file under the solution explorer
Select the designer file under the solution explorer and use the context menu to bring up the file
I have an evaluation copy of the VS 2010 Team and it displays exactly the same behavior when utilizing my project
I have tried a backup copy of the project but it displays the same problem in both the Express and Team versions
I have been all over the MSDN, VS forum, and the internet at large and not found any solutions
When I first started the project I did rename the Form1.cs file from within the solution explorer because I wanted to have a different name than Form1 for the application. When doing this it asked the following:
"You are renaming a file. Would you like to perform a rename in this project of all references to the code element 'projectname'?
I replied yes. So now I don't have a Form1.cs file but a file with my "projectname.cs" and the designer file named "projectname.Design.cs. This is the only change that I made that I can think of that might be relevant. I did try renaming it back to Form1.cs but that also did not resolve the problem.
I have to say that I am new to using Visual Studio. So far I like it quite a bit but I am dead in the water right now and unless I can get this resolved I will lose two weeks worth of work. Any assistance would be greatly appreciated.
Thanks,
-Tom
Additional Details:
Windows 7 Professional SP1 32 bit x86
Microsoft Visual Studio 2010
Version 10.0.40219.1 SP1Rel
Microsoft .NET Framework
Version 4.0.30319 SP1Rel
Installed Version: C# Express
Microsoft Visual C# 2010 01014-169-2560017-70726
Microsoft Visual C# 2010
Microsoft Visual C# 2010 Express - ENU Service Pack 1 (KB983509) KB983509
This service pack is for Microsoft Visual C# 2010 Express - ENU.
If you later install a more recent service pack, this service pack will be uninstalled automatically.
I realise this is quite an old thread now, but I just had something similar happen in one of my projects in VS2010. There was no way to edit the Form in the form designer.
In the end tracked it down to a problem in the .csproj file. There is a item group that specifies what is compiled. Within the item group tag, for my form it had ...
<Compile Include="AreaChart.cs" />
it needed...
<Compile Include="AreaChart.cs">
<SubType>Form</SubType>
</Compile>
The SubType XML tag was missing. A quick change to the project file and I could edit the form again.
Just one more way to screw up the form designer - I had something like this:
namespace MyProgram
{
public struct SomeStruct {
int a,b;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Turns out you can't put the SomeStruct definition there even though everything works fine and there were no errors. Once I moved it, I was able to see the designer view again.
Again it is an old thread but web interest indicates that numerous people are still having this problem and it has not been solved.
I am using VS2012 and had the problem when I copied a library control which spawns 3 forms into a separate library for further development. All controls in the forms displayed perfectly when run but the designer displayed a blank form.
The problem was not as above (that was intact) but, thanks to this hint, I added a new form and compared the entries in the .csproj file. For each of the copied forms, an entry akin to the following was showing:
<Compile Include="MyForm.Designer.cs" />
What was needed was:
<Compile Include="MyForm.Designer.cs">
<DependentUpon>MyForm.cs</DependentUpon>
</Compile>
Adding these manually for each form solved the problem.
Make sure you don't have any class declared before your designer partial class. The .cs file needs to have the partial designer class as the first class in the file.
public partial class frmWidget : Form
{
}
public class SomeClass
{
}
Now SomeClass cannot be on top of the partial frmWidget class. Once you re-arrange that then it will automatically fix the designer issue and no need to restart VS or re-create the form. I hope this helps.
Tried all the suggestions mentioned in this question, nothing worked for Visual Studio Community 2017
no Shift-F7 shotcut
no context menu item visible
no double clicking on MainForm.cs
no wrong class order inside MainForm.cs
no wrong entries in mysolutionname.csproj file
But this worked for me
Right click your MainForm.cs file (or 'Form.cs' depending how you named it)
'Exlude from project'. This temporarily removes MainForm.cs, MainForm.Designer.cs and MainForm.resx
Use 'Add ยป Existing item' and select MainForm.cs. This will re-include all three files.
Of course you should add them back to the same folder as they were before
Double click on MainForm.cs and Design view magically openes
I had the same problem but mine was caused on VS2012 by dragging and dropping some of the forms into different folders.
David's solution above was helpful towards fixing it, but didn't go all the way.
If you have the form in a sub-folder the Compile Include line should read like this:
<Compile Include="SubFolderName\MyForm.Designer.cs">
<DependentUpon>MyForm.cs</DependentUpon>
</Compile>
Please note that the sub-folder name is needed in the first line but not in the second line.
I also had to change the EmbeddedResource line further down the file to associate the resx file:
<EmbeddedResource Include="SubFolderName\MyForm.resx">
<DependentUpon>MyForm.cs</DependentUpon>
</EmbeddedResource>
Again, note that the first line mentions the sub-folder whereas the second line doesn't.
Thanks for the thoughts. I just completely reinstalled both the Express and evaluation copies of the Team versions of VS 2010 and it is now magically working. I have no idea what caused the problem nor what fixed it but I'm now back on track. I probably should have done that before asking on this forum.
I had the same problem with VB. Thanks for the hints. I found the same issues in the .vbproj with my UserControl.
I had this in the .vbproj file:
<Compile Include="AddCommunicationTask.vb" />
And changed it to this:
<Compile Include="AddCommunicationTask.vb">
<SubType>UserControl</SubType>
</Compile>
When I reloaded it I was still having problems and realised that I had written another class in above the form class, this was causing the error. The form class must be first. All I had to do was move my class down and make the form class the top one.
I hope this helps others - good luck!
p.s - I am using vs Community 2015
I had the same issue since I had created the project as a C# Windows Form App (.NET Core) I decided to create it as a C# Windows Form App (.NET Framework) and I can now see and use the Designer. Note the difference between using 'Core' vs 'Framework'
I hope this helps you out.
I realise this is a really old thread now, but I had the same problem and it turns out there is another node that can go astray in the csproj file; the form .resx has an EmbeddedResource node and mine was missing the subnode Designer. This allowed me to see the form code again in the IDE.
I had a similar issue, but not quite the same.
I copied a form from one project to another (using windows explorer) and then added it to the new project by right clicking on the project in VS and choosing > Add > Existing Item.
My mistake was to add all three files for the form (.cs, .designer.cs and .resx). I should have only added the .cs file.
Although this is quite an old thread, I want to share my case and the solution.
I had the same problem but I used Telerik WinForms component for my Form. I couldn't see the designer until I realize that the design-time capabilities of RadControls for WinForms are implemented in the Telerik.WinControls.UI.Design.dll assembly. Then I add the reference and now the designer shows up.
I had the same problem and I got the (Design) window back by double clicking the Form1.cs file in the Solution Explorer (not the Form1.Designer.cs file).
I accidentally deleted my Form1[Design] file. Double clicking the Form1.h file did nothing. The program ran and the Buttons TextBoxes and Labels were all there just no Form1[Design] was visible to edit. I fixed it Under: Header Files select Form1.h and go to Form1.H file properties. Under File Type from the drop down change it to "C++ Form" I closed and restarted the program. You should now be able to double click Form1.h and see your Form1[design].
I had the same issue in VS 2010. I was not able to view the designer form(Connect.CS).
i already had the
<Compile Include="Connect.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Connect.Designer.cs">
<DependentUpon>Connect.cs</DependentUpon>
</Compile>
tags in the ItemGroup Tag, but still i wasn't able to view the Designer.
I was using name space directive 'Using RFCOMAPILib', which already had a Form element in it and it clashed with the System.Windows.Forms.Form.
After replacing 'public partial class Connect : Form' to 'public partial class Connect : System.Windows.Forms.Form' im able to view the Designer now.