C# custom Windows Forms base class and designer - c#

I am trying to implement the GUI part of a plug-in, which means that I have to inherit from a custom base class (which inherits from UserControl) included with the plugin assembly.
When implementing my own control, I would normally inherit from UserControl and going to the designer would be really straightforward (just double clicking on the solution explorer).
In order to be able to work with the designer, I do a first implementation using UserControl as base class.
The problem is that as soon as I change the base class into ApplicantTabControlPlugin (the custom base class provided by the plugin), I cannot open the designer for this control anymore. I.e., if I close the designer, it seems it is gone forever.
Is there any way to prevent this behaviour?

You should add
<SubType>Component</SubType>
to the project file entry of your base class.

Related

Windows Form multiple designer for a control

I made a custom designer for a control. The problem is that the base class has a designer, too. Since it's marked as internal, I can't inherit from it. Is there a way to force the designer to use multiple designers for a control?

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

Visual Studio 2010: Add new class file that inherits from existing class?

Is there a way in Visual Studio 2010 to add a new file (class) to a project that inherits from an existing base class, abstract or otherwise? This seems like a simple concept but I can't find a way to do it.
I'd like to be able to for example right click a class name in the IDE and and select "add new inheriting class" or something similar. Just one of those things that would save a lot of repetition. Ideally it would implement (generate stubs) for abstract members, etc.
Does anyone know a way to do this either through stock VS2010 or ReSharper? I've dug through both and can't find anything. Interestingly enough, Resharper will allow for generating a superclass, but not a subclass..
I don't think it's actually faster to right click -> add base class then to add a class file and type : Base behind it.
If you want to implement an abstract class you can right click on the base class and select "Implement Abstract Members"
You can also use the Class Diagram tool (right click a project and select View Class Diagram) then add a class and drag an inheritance arrow from the toolbox onto it.
If you are adding Form or User control, this option is available in VS2010, under Windows Forms Tab.
You could create a new item template for the base class(es) you find yourself using most often. Or you could extend Visual Studio and roll your own.

Share a (container)control from a (custom)control?

Soo... I am making a control like the Windows Update "panels".
Everything's fine up to the "container" part.
What I want to do is to allow the designer to place controls in a Panel which is inside my control.
(The panel's variable is held in my control's class and inside the control itself.)
How do I bypass this?
As a reference, you might want to try out this AeroWizard Control, which does this pretty well.
(Yes, I have looked at it and didn't find a clue but custom designers!)
As a side note, I'd rather not make a complicated designer class...
If you don't want to create a custom designer class, you should implement your control as a templated custom control, preferably inheriting from CompositeControl.
There doesn't seem to be a way to do the same in Windows Forms without a custom designer class. However, there's a nice, short, working example of such a designer here.

Categories

Resources