Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Visual Studio 2008 Windows Forms designer to render a Form that implements an abstract base class?

I engaged a problem with inherited Controls in Windows Forms and need some advice on it.

I do use a base class for items in a List (selfmade GUI list made of a panel) and some inherited controls that are for each type of data that could be added to the list.

There was no problem with it, but I now found out, that it would be right, to make the base-control an abstract class, since it has methods, that need to be implemented in all inherited controls, called from the code inside the base-control, but must not and can not be implemented in the base class.

When I mark the base-control as abstract, the Visual Studio 2008 Designer refuses to load the window.

Is there a way to get the Designer work with the base-control made abstract?

like image 418
Oliver Friedrich Avatar asked Oct 25 '09 12:10

Oliver Friedrich


People also ask

How do I view Form Designer in Visual Studio?

From the Solution Explorer window select your form, right-click, click on View Designer. Voila! The form should display. Save this answer.


3 Answers

I KNEW there had to be a way to do this (and I found a way to do this cleanly). Sheng's solution is exactly what I came up with as a temporary workaround but after a friend pointed out that the Form class eventually inherited from an abstract class, we SHOULD be able to get this done. If they can do it, we can do it.

We went from this code to the problem

Form1 : Form

Problem

public class Form1 : BaseForm
...
public abstract class BaseForm : Form

This is where the initial question came into play. As said before, a friend pointed out that System.Windows.Forms.Form implements a base class that is abstract. We were able to find...

Proof of a better solution

  • Inheritance Hierarchy:

    • System.Object
      • System.MarshalByRefObject (public **abstract** class MarshalByRefObject)
        • System.ComponentModel.Component
          • System.Windows.Forms.Control
            • System.Windows.Forms.ScrollableControl
              • System.Windows.Forms.ContainerControl
                • System.Windows.Forms.Form

From this, we knew that it was possible for the designer to show a class that implemented a base abstract class, it just couldn't show a designer class that immediately implemented a base abstract class. There had to be at max 5 inbetween, but we tested 1 layer of abstraction and initially came up with this solution.

Initial Solution

public class Form1 : MiddleClass
...
public class MiddleClass : BaseForm
... 
public abstract class BaseForm : Form
... 

This actually works and the designer renders it fine, problem solved.... except you have an extra level of inheritance in your production application that was only necessary because of an inadequacy in the winforms designer!

This isn't a 100% surefire solution but its pretty good. Basically you use #if DEBUG to come up with the refined solution.

Refined Solution

Form1.cs

public class Form1
#if DEBUG
    : MiddleClass
#else 
    : BaseForm
#endif
...

MiddleClass.cs

public class MiddleClass : BaseForm
... 

BaseForm.cs

public abstract class BaseForm : Form
... 

What this does is only use the solution outlined in "initial solution", if it is in debug mode. The idea is that you will never release production mode via a debug build and that you will always design in debug mode.

The designer will always run against the code built in the current mode, so you cannot use the designer in release mode. However, as long as you design in debug mode and release the code built in release mode, you are good to go.

The only surefire solution would be if you can test for design mode via a preprocessor directive.

like image 83
smelch Avatar answered Oct 16 '22 13:10

smelch


@smelch, There is a better solution, without having to create a middle control, even for debug.

What we want

First, let's define the final class and the base abstract class.

public class MyControl : AbstractControl
...
public abstract class AbstractControl : UserControl // Also works for Form
...

Now all we need is a Description provider.

public class AbstractControlDescriptionProvider<TAbstract, TBase> : TypeDescriptionProvider
{
    public AbstractControlDescriptionProvider()
        : base(TypeDescriptor.GetProvider(typeof(TAbstract)))
    {
    }

    public override Type GetReflectionType(Type objectType, object instance)
    {
        if (objectType == typeof(TAbstract))
            return typeof(TBase);

        return base.GetReflectionType(objectType, instance);
    }

    public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
    {
        if (objectType == typeof(TAbstract))
            objectType = typeof(TBase);

        return base.CreateInstance(provider, objectType, argTypes, args);
    }
}

Finally we just apply a TypeDescriptionProvider attribute to the Abastract control.

[TypeDescriptionProvider(typeof(AbstractControlDescriptionProvider<AbstractControl, UserControl>))]
public abstract class AbstractControl : UserControl
...

And that's it. No middle control required.

And the provider class can be applied to as many Abstract bases as we want in the same solution.

* EDIT * Also the following is needed in the app.config

<appSettings>
    <add key="EnableOptimizedDesignerReloading" value="false" />
</appSettings>

Thanks @user3057544 for the suggestion.


like image 83
jucardi Avatar answered Oct 16 '22 12:10

jucardi


@Smelch, thanks for the helpful answer, as I was running into the same issue recently.

Following is a minor change to your post to prevent compilation warnings (by putting the base class within the #if DEBUG pre-processor directive):

public class Form1
#if DEBUG  
 : MiddleClass 
#else  
 : BaseForm 
#endif 
like image 11
Dave Clemmer Avatar answered Oct 16 '22 14:10

Dave Clemmer