winforms icon indicating copy to clipboard operation
winforms copied to clipboard

.NET 5.0 OnCreateHandle override isn't called

Open chris-steema opened this issue 5 years ago • 8 comments

  • .NET Core Version:

Microsoft.NETCore.App.Ref v5.0.0 Microsoft.WindowsDesktop.App.Ref v5.0.0

  • Have you experienced this same bug with .NET Framework?:

No.

Problem description:

When I build this control in .NET Framework and then drag it onto a .NET Framework Windows Form design surface, OnCreateHandle is called and the MessageBox is shown. However, when I build this control in .NET 5.0 (net5.0-windows) with UseWindowsForms true and then drag it onto a .NET 5.0 Windows Forms design surface, OnCreateHandle is not called and the MessageBox does not show.

Expected behavior:

I would expect OnCreateHandle to be called in .NET 5.0 in the same way as it is called in .NET Framework 4.6

Minimal repro:

[Designer(typeof(TestDesigner))]
public class NETFrameworkControl : Control
{
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        e.Graphics.FillRectangle(Brushes.Red, e.ClipRectangle);
    }

    public class TestDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        protected override void OnCreateHandle()
        {
            base.OnCreateHandle();
            MessageBox.Show("OnCreateHandle");
        }
    }
}

chris-steema avatar Jan 11 '21 10:01 chris-steema

The issue can reproduce from .NET SDK 3.1 to .NET SDK6.0, please see below screenshot. Application: Core4451.zip Core4451

Zheng-Li01 avatar Jan 13 '21 08:01 Zheng-Li01

.NET 6-preview5 was out almost a month ago. Any updates on this issue?

bairog avatar Jul 09 '21 04:07 bairog

No.

Note also that the designer is separate from the .NET release and version. The designer is part of Visual Studio and updates with each new VS preview.

RussKie avatar Jul 09 '21 05:07 RussKie

Using the code on the issue itself, I've just run it using .NET 6 SDK version 6.0.100-rc.2.21505.57 in Microsoft Visual Studio Enterprise 2022 Preview (64-bit) Version 17.0.0 Preview 5.0, using .NET 6 as the target Framework in the Core4451 project. Unfortunately the issue can still be reproduced - it works in .NET Framework 4.8, but doesn't work in .NET 6.

Note also that the designer is separate from the .NET release and version. The designer is part of Visual Studio and updates with each new VS preview.

Is there approximate timeframe for fixing that in Visual Studio 2022?

bairog avatar Oct 14 '21 08:10 bairog

@Zheng-Li01 @RussKie @KlausLoeffelmann @dreddy-work Any updates? What is an approximate timeframe for fixing that in Visual Studio 2022?

bairog avatar Jan 10 '22 10:01 bairog

There is a breaking change difference in how Designers work in Framework and .NET, so neither is this a bug, nor is it supposed to work this way.

To create a Designer for VS and .NET, you would need to use the WinForms Designer SDK. The reason is (simplified), that the Designer needs to run in the context of the Form (or the UserControl), which is .NET and not Framework. But VS is Framework. So, for the .NET Designer, there are two processes: The Client Process (Visual Studio, Framework) and a dedicated Server process (.NET) which is called the DesignToolsServer. So principally, and depending on what you need to do, you'd need to provide a NuGet which VS picks up, which holds the code for both the Server and the Client Process. And since the control and most part of the Designer logic is created in the Server process, anything Dialog-UI related needs to be marshalled from the Server process, which is not supposed to handle any dialogs due to potential dead lock concerns, back to the VS-Client.

Docs for the SDK are in the making, they are on our roadmap for the first quarter of this year.

KlausLoeffelmann avatar Jan 11 '22 18:01 KlausLoeffelmann

To create a Designer for VS and .NET, you would need to use the WinForms Designer SDK. The reason is (simplified), that the Designer needs to run in the context of the Form (or the UserControl), which is .NET and not Framework. But VS is Framework. So, for the .NET Designer, there are two processes: The Client Process (Visual Studio, Framework) and a dedicated Server process (.NET) which is called the DesignToolsServer.

Well looks like it is one more big reason to finally move Visual Studio 2022 to .NET 6. So both processes will be .NET..

P. S. State of the Windows Forms designer for net applications article on DevBlogs

bairog avatar Jan 14 '22 07:01 bairog

Closing as there is nothing actionable here.

RussKie avatar Feb 21 '22 02:02 RussKie