VS2012 designer cannot open form after inheriting from another form - c#

I have a base form BaseForm which I use for saving settings. This form have no controls in it and it's opened in the designer as an empty form. However, when I inherit this form, the designer refuses to open the newly created form in the designer (there's no button View in Designer). What may be the problem ? Thanks

The problem is with the .csproj file.
Every form has a signature in the file like this
<Compile Include="Opers\MyForm.cs">
<SubType>Form</SubType>
</Compile>
The SubType element says that this is actually a form and in my file this was deleted for some reason.

I would highly recommend to avoid inheriting forms whenever possible. For saving settings, this is surely avoidable: make yourself an FormSettingsSaver class or something like that and pass it the form as argument when loading/closing, etc.
Inheriting forms can make a lot of headaches and MS isnĀ“t really putting any work in WindowsForms and/or the Designer.
edit: if you want to stick with inheriting forms, check the link from rene in the comment. Most designer problems can be avoided with checking for DesignMode. You could also try to comment out statements until you find the culprit. All code in the constructor and (i think) OnLoad is executed when the form is displayed in the Designer, so there would be a good place to start.

Related

A partial class has multiple form

When I write a winforms application, I tend to create an Implementation.cs file for each form.
Say I have Form1.cs, I'd create a new file called Form1.Implementation.cs starting with partial class Form1.
Form1.cs just contains all the event callback methods (what the designer has done), everything else goes to Form1.Implementation.cs. It helps me write more readable code.
I wanted Form1.Implementation.cs to be a "subfile" just like Form1.Designer.cs is, so I edited .csproj file.
<Compile Include="Form1.Implementation.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
After reload, however, Visual Studio automatically adds <SubType>Form</SubType> right after DependentUpon element. Doubleclicking Form1.Implementation.cs doesn't show code but a designer with another initial empty form.
It's like
"class Form1, which ISA Form, is(?) multiple forms."
I tried adding DesignerCategory attribute to class Form1, but it affects Form1.cs, as well.
Well, hitting 'Shift-F7' or 'Ctrl-Shift-0' is not a big deal.
I wonder if...
it's a glitch of Visual Studio,
the secondary(?) form really exists somehow,
it's going to blow up my winforms project someday
The only way of achieving what you tried is by adding <DependentUpon>, which you already know. Now Visual Studio automatically adds the <SubType>Form</SubType> for any class derived from Form. Since your Form1.cs probably contains the line public partial class Form1 : Form, this is where the SubType is coming from.
The other files - Form1.Designer.cs and Form1.Implementation.cs may contain only partial class Form1, but since partial class definitions across multiple files are still effectively one class definition, Visual Studio detects that it still inherits Form. I believe you may already know that, but just in case here's the MSDN article about the partial keyword. Don't worry about there being multiple instances of Form in this scenario. Remember this still is just one class - Form1, no mater over how many files you spread it.
In the end, all code files containing classes (even partial!) inheriting Form (or UserControl) are automatically opened in the Designer. This behaviour is by design.
The solution here is simple - either make a code file defining a separate class not based on Form, or just use F7 to view the code of that file in Solution Explorer, however annoying that may seem. It doesn't matter if that code file is <DependentUpon> anything or not. Only the inheritance of Form or UserControl matters.
The best solution though, in my opinion, would be to stick to what Visual Studio is giving you:
Designer-generated code stays in Form1.Designer.cs
Your code (what you put in Implementation), goes in Form1.cs (hit F7 to view that code instead of going to the designer)
This is an approach my team has been sticking to for a few years now. It provides a basic means of separation of Designer code and hand-coded actions. To better separate your code, use a design pattern such as MVP, as suggested by Simon Whitehead in the comments.

Designer.cs file not generated on sub-classing a subclass of System.Windows.Form

I had a Form "ParentForm" Designed in C# VS2010 with two buttons.
I wanted five forms to have the same two buttons, so i decided to write five *.cs files(subForm1.cs,subForm2.cs...subForm5.cs) derived from the "ParentForm" as base class.
Now VS2010 shows these derived classes with a form icon(ie recognizes as Forms), but does not generate a .designer.cs file for it. So the problem I am facing is that, whenever I drag a
Control into derived class form say subForm1.cs , VS2010 puts the auto-generated code into my subForm1.cs instead of subForm1.Designer.cs. Although I tried manually creating a file named subForm1.Designer.cs (which also gets detected and is put under the hierarchy of the Form icon in solution explorer) but, still the auto-generated code goes to the subForm1.cs file. How do
I tell VS2010 to patch subForm1.cs+subForm1.Designer.cs+subForm1.resx as one form subForm1.
If you want to have the designer file, Add new Windows Forms.
Then replace the inheritance from Form class to your ParentForm class.
This way any new controls you add to your child form should get added in the designer file.
Also please go through this link on Visual Inheritance

How can I add my own code along with the code generated by the form designer?

When I am working in Visual Studio, and use the designer, VS manages the code for creating and positioning the controls.
But what if I want to add my own code there?
Say I want to use a string variable as the name for my form, or for the default text in a box? I know I can set this in the form_load function, but what if I want to do it in the designer code page?
When the designer generates this code, it is arranged in such a way that the designer can add to it. How can I add my own code, or manage parts of the code, without interfering with the functionality of the designer?
Would it work if I moved parts of the code to a different file? How can I do that?
Basically, I want to Have My Designer and Code it Too!
Visual Studio should create the designer as a "partial" class. Just create another file that is also another part of the same "partial" class. It will be separate file, but same class. Also, the autogenerator won't overwrite your code this way.
Right click on the form and click on 'View Code'. Adding your code there will not interfere with the designer don't worry!
To change the name or any property of a textbox (or any control) simply click on the textbox in the Form/Designer and change any of its property in the properties box, usually at the bottom right of the screen.

*.Designer.cs file is missing

I'm using Visual Studio 2010 and when I create a form, I get a FormName.Designer.cs file with all the auto generated code.
This is exactly what I want, however, when I do the following:
Created an empty class.
Subclass System.Windows.Forms.Control
Add a System.Windows.Forms.ImageList to the designer.
Add images to the ImageList in the designer.
All of the auto generated code gets dropped in my class file and I don't get a *.Designer.cs file.
How do I get VS to always use a *.Designer.cs file?
Update
Declaring a class partial and subclassing UserControl is not sufficient. I had to select "UserControl" when I created a new Item, then I got a designer.cs file.
This is normal and the way the designer worked in versions of Visual Studio before VS2005, versions that did not yet support partial classes. You only get the separate Designer.cs file for a class that's derived from Form or UserControl and you declared them with the partial keyword.
Not 100% sure what's the underlying reason, but surely it has something to do with the very simple designer for the Control class. The designers for Form and UserControl are much fancier.
Nothing actually goes wrong so this is not a real problem. There is very little bang for the buck, especially since it is so uncommon to actually need another component or control when you create a custom control. Getting the Properties window to work is a convenience but, in my personal experience, isn't worth the considerable annoyance of getting the designer page by default when you double-click the class in the Solution Explorer window. I lost count of the number of times I shouted "crud!" at the machine.

Icon not shown at runtime in derived Windows form

I have a C# app where one of my Form icons is visible in the designer but reverts to the default icon at runtime. The form in question is a derived form, with no .designer.cs file of its own, and the derived form .cs file is empty. The form icon displays in the designer correctly, but at runtime the icon is missing.
The derived form is in a different project from the base form. The base form icon was added via the designer, and no modifications were made to the base .designer.cs file.
Any ideas what might be causing this?
After mucking about for a bit, I think my problem is related to this bug in VS: https://connect.microsoft.com/VisualStudio/feedback/details/106264/mdi-form-icon-formborderstyle-windowstate-maximized#tabs
I got around my issue by re-adding the Icon to the derived form (using the designer), and then also had to add a _Load handler with the following
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MyForm));
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
Does the job, though I don't enjoy hacky workarounds!

Categories

Resources