A partial class has multiple form - c#

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.

Related

c# windows forms create generic form [duplicate]

I'm using vb.net (vs2010). I'm moving some winforms to a dll. I have a form that inherits from the one which has some subs and functions (like a test app).
My original form is: (.designer)
Partial Class Form1(Of T)
Inherits System.Windows.Forms.Form
....
End Class
Form itself contains code and a toolbar.
My test form is: (.designer)
Partial Class TestForm
Inherits Form1(Of Class1)
I get "Cannot create an instance of Form1`1[T] because Type.ContainsGenericParameters is true" when VS try to load the designer. App is usable. I can build and run the project without errors, but I need to add controls and some code to each new form.
I tried many ways:
Visual Studio 2008 Winform designer fails to load Form which inherits from generic class
How can I get Visual Studio 2008 Windows Forms designer to render a Form that implements an abstract base class?
http://www.codeproject.com/Questions/419770/Csharp-reflection-GetValue-from-a-field-in-generic
http://madprops.org/blog/Designing-Generic-Forms/
All examples are for C#, and I don't know if I'm missing something...
Is this a bad design ? I know this is a VS bug but still it seems everyone fixed it by these links.
EDIT:
I'm building a DLL. Form1 is on this DLL and TestForm is in a new project. Those links works if I'm in the same project (a.k.a. the dll).
Thanks!
Is this a bad design ? I know this is a VS bug
Bad design, not a VS bug. What you are trying to do is fundamentally incompatible with the way the Winforms designer works. It has strong WYSIWYG support, the designer creates an instance of the form's base class and allows code in that base class to run at design time. Which is why, for example, you can set the BackgroundImage property and it is immediately visible in the designer. The Form.OnPaintBackground() method paints it. The designer is not involved at all, it just sets the property.
To make that work, it must be able to create the base class object. It can't in your code, it doesn't know what kind of T to use. Not an issue when you design Form1, the T isn't needed yet since it derives from Form and creating an instance of Form is not a problem. Big issue when you design TestForm.
You'd probably argue that it should use Class1 as the T. It doesn't, the odds that it can use Reflection to discover the generic type argument from TestForm are exceedingly low. That requires the type to be compiled first. That's a chicken-and-egg problem at design time, the TestForm class gets compiled after you design it, not before or while you design.
It's not like you completely cannot use your approach. It builds and runs just fine. You just have to live without design time support for TestForm. That's usually a deal breaker, you have to re-consider your design.

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

*.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.

VS2012 designer cannot open form after inheriting from another form

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.

Why does my class suddenly have a 'designer'?

I just finished adding and removing different database models (I was trying to figure out which one I should be using for this project) then after playing around for a while I noticed one of my classes's icon changed from what is shows beside my Calculations.cs class in the first image to the Balance.cs icon.
The Balance.cs now has this Designer component so when I double click on it I see my second screen shot. This seems to be allowing me to add components from the toolbox to my class. There are actually two classes within my Balance.cs. This Designer thing is only affecting/interacting with one of them (it inherits from SerialPort).
I don't really know what changed or what I did to make this happen and ctrl+z is not being my friend here. How do I change Balance.cs back to a regular class with no designer component?
Thanks
If any of the classes in a source file inherit - either directly or indirectly - from System.ComponentModel.Component (such as SerialPort), Visual Studio will provide design-time support to you. This is sometimes unwanted behaviour, and you can safely ignore it in most cases.
If it really bothers you, you can decorate your class with the [DesignerCategory] attribute (set the category to an empty string).

Categories

Resources