question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.NET 5.0 OnCreateHandle override isn't called

See original GitHub issue
  • .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");
        }
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
KlausLoeffelmanncommented, Jan 11, 2022

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.

1reaction
bairogcommented, Oct 14, 2021

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - function overrides not calling
I have a class with a virtual function, and a derived class that overrides it. When I call the function, the override isn't...
Read more >
Override method not being called
Hi everyone, I have two methods for using a pressure plate to activate effects on another object. The first script is a component...
Read more >
Knowing When to Use Override and New Keywords - C# ...
Because BaseClass now has a Method2 method, a second calling statement can be added for BaseClass variables bc and bcdc , as shown...
Read more >
C# design to force the virtual method to be called from ...
Or other times where you call base and the derived class didn't need to because it overrides all the base values rather than...
Read more >
Select which .NET version to use
Consider the following scenario: The application specifies that 5.0 is required. When run, version 5.0.* isn't installed, however, 5.1.0 is.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found