Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2010: How to avoid Windows Forms designer problems when working with inherited user controls?

Problem: The Windows Forms designer does not work for an inherited user control when the base class is implementing an interface from another assembly.

Platform: VS 2010 SP1, .NET 4.0 Framework

Error:

The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: MyControl --- The base class 'MyBaseControlLib.MyBaseControl' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager) at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager) at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager) at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload)

I have a solution with 3 class library projects:

MyInterfaceLib:

namespace MyInterfaceLib
{
    public interface IMyInterface
    {
        void Foo();
    }
}

MyBaseControlLib:

namespace MyBaseControlLib
{
    using System.Windows.Forms;
    using MyInterfaceLib;

    public partial class MyBaseControl : UserControl, IMyInterface
    {
        public MyBaseControl()
        {
            InitializeComponent();
        }

        public void Foo()
        {
        }
    }
}

MyDerivedLib:

namespace MyDerivedControlLib
{
    using MyBaseControlLib;

    public partial class MyControl : MyBaseControl
    {
        public MyControl()
        {
            InitializeComponent();
        }
    }
}

Although the designer works for MyBaseControl it does not work for MyControl. If MyBaseControl does not implement IMyInterface, the designer also works for MyControl.

Any ideas?

Thanks, Robert

like image 704
Robert Hahn Avatar asked Mar 23 '11 13:03

Robert Hahn


People also ask

Which base class is used for all the Windows controls in window form application?

Form. This base class provides the functionality you need to design Windows Forms applications.

What design window do you use to add controls to a form?

The primary way a control is added to a form is through the Visual Studio Designer, but you can also manage the controls on a form at run time through code.

Will WinForms be deprecated?

WinForms won't be deprecated until Win32 is ... which could be quite sometime! WPF on the other hand has few direct dependencies on Win32 so could potentially form the basis of a "fresh start" UI layer on a future version of windows.


1 Answers

We had the same issue. We used a workarround by creating a MyControlDesign class that is inherited by MyControl class.

 public partial class MyControl : MyControlDesign {
   public MyControl()
  {
      InitializeComponent();
  }
}

public partial class MyControlDesign : MyBaseControl
{
  public MyControlDesign ():base()
  {
  }
}
like image 119
Niki Avatar answered Oct 02 '22 16:10

Niki