How to write WinForms Designers in NET 6.0

382 Views Asked by At

I need to create WinForms project with components under the .net library version 6.0. But when I create the project and the ControlLibrary the Visual Studio Form designer doesn’t use ControlDesigners from the ControlLibrary, even though I correctly bind designers to the component.

  [Designer(typeof(MyControlDesigner))]
  [ToolboxItem(true)]
  public class MyControl : Control 
  {

    protected override void OnPaint(PaintEventArgs e)
    {
      e.Graphics.FillRectangle(SystemBrushes.Info, new Rectangle(0, 0, Width, Height));
      e.Graphics.DrawString("MyControl", Font, SystemBrushes.InfoText, new PointF(0, 0));
    }
  }

I have created some ActionLists but they are not showing up in the component at design-time.

namespace WinFormsControlLibrary1
{
  internal class MyControlDesigner : ControlDesigner
  {
    private DesignerActionListCollection actionLists;

    public override DesignerActionListCollection ActionLists
    {
      get
      {
        if (actionLists == null)
        {
          actionLists = new DesignerActionListCollection();
          actionLists.Add(new DataAxisGridActionList(Component));
          actionLists.AddRange(base.ActionLists);
        }
        return actionLists;
      }
    }
  }

  public class DataAxisGridActionList : DesignerActionList
  {

    public DataAxisGridActionList(IComponent component) : base(component)
    {
    }

    public override DesignerActionItemCollection GetSortedActionItems()
    {
      DesignerActionItemCollection items = new DesignerActionItemCollection();
      items.Add(new DesignerActionMethodItem(this, "Action1", " Action 1", true));
      items.Add(new DesignerActionMethodItem(this, "Action2", "Action 2", true));
      return items;
    }

    public void Action1()
    {
      MessageBox.Show("Action 1");
    }

    public void Action2()
    {
      MessageBox.Show("Action 2");
    }
  }

}

For the test, I created a similar project and library under the .NET Framework 4.7.2 and the designer works correctly there.

enter image description here

enter image description here

I am also attaching a link to demo projects.

https://github.com/dmitrybv/WinForms-Net5-Designers

https://github.com/dmitrybv/WinForms-Net-Framework-4.7.2-Designers

1

There are 1 best solutions below

3
On

For now, given the not yet completely finalized nature of the Design-Time support in the new Out-Of-Process architecture of the WinForms Designer, probably the simpler solution is to install the WinForms Designer Extensibility SDK.

It's available via NuGet Package Manager, using the name: Microsoft.WinForms.Designer.SDK

or paste in the Package Manager Console:

NuGet\Install-Package Microsoft.WinForms.Designer.SDK -Version 1.6.0

or add the Package Reference to the Project's configuration file in an existing <ItemGroup> of <PackageReference> (or create a new one):

<PackageReference Include="Microsoft.WinForms.Designer.SDK" Version="1.6.0" />

A description of the current state of the Designer support can be found here:
State of the Windows Forms Designer for .NET Applications


Add the required using directives in the code file that contains the custom Designer (ControlDesigner and DocumentDesigner support):

using Microsoft.DotNet.DesignTools.Designers; 

For Behavior Services and Adorners (glyphs) that can receive hit test and paint messages:

using Microsoft.DotNet.DesignTools.Designers.Behaviors