Tomsaw
Tomsaw

Reputation: 31

.Net 6 Windows Forms Designer deletes own user controls

I transferred an application from .net fw 4.8 to .net 6. A form contains some simple own UserControls (only containing 2 labels each). When I open the designer the controls are not being displayed anymore. When I run the application the controls are being displayed properly. When I make some change to the form (e.g. changing any property value), the designer deletes all of the custom user controls. How can I prevent him from doing this? What can I do to make the user controls to be visible in designer again ?

Upvotes: 1

Views: 878

Answers (4)

Brendan Gooden
Brendan Gooden

Reputation: 1561

In the latest version of .NET 8 / Visual Studio 2022 the WinForms Designer uses an out-of-process style which also means that the following code now requires updating.

/// <summary>
/// Check if the current form is in design mode.
/// </summary>
/// <returns></returns>
public static bool IsInDesignMode(this Control control)
{
    return LicenseManager.UsageMode == LicenseUsageMode.Designtime;
}


/// <summary>
/// Check if the current form is in design mode.
/// </summary>
/// <returns></returns>
public static bool IsInDesignMode(this Control control)
{
    ArgumentNullException.ThrowIfNull(control);

    return LicenseManager.UsageMode == LicenseUsageMode.Designtime ||
           control.Site is { DesignMode: true } ||
           (control.GetType().GetProperty("DesignMode", BindingFlags.NonPublic | BindingFlags.Instance)
               ?.GetValue(control) as bool? ?? false) ||
           Assembly.GetEntryAssembly().GetName().Name == "DesignToolsServer";
}

And usage inside a form constructor

public NotesControl()
{
    InitializeComponent();

    if (this.IsInDesignMode())
    {
        return;
    }

    // Do other stuff in here at runtime. 
}

Refer to this Github Issue -> https://github.com/dotnet/winforms/issues/10960

Upvotes: 0

Horst Walter
Horst Walter

Reputation: 14081

I did have the same issue, my own user controls were removed whenever I edited the main form. In my case I was able to resolve the issue by using a guard in my user controls' constructors:

if (DesignMode) { return; }

Another user in a MS forum did solve the issue by changing his classes from internal to public.

Check this thread as well: How do I keep Visual Studio's Windows Forms Designer from deleting controls?

Upvotes: 0

HellFire
HellFire

Reputation: 16

What "Visual Studio" version you use? For VS 2022 (Preview): Check "Options > Environment > Preview Features" - "Use the preview Windows Forms out-of-process designer for .NET Framework apps (requires restart)"

Uncheck it, Restart VS and check your Forms/UserControls again.

Upvotes: 0

Tomsaw
Tomsaw

Reputation: 31

The problem was solved by moving the UserControl to another assembly than the one that contained the form. The designer saw an "outdated" version of the UserControl and could not use it.

Upvotes: 1

Related Questions