We have a Winforms project upgraded to .NET 8. The conversion had only a few problems that where easily fixed, so we continued on this path.
However, now I found a problem that I suspect is a bug in Winforms designer on .NET 8.
I have a custom control, and a panel on that control marked as a “dropzone”, so you can drop controls on this custom control in the designer. This worked well until the move to .NET 8.
Here is a snippet of the custom control’s code:
[Designer(typeof(UserControlDesigner))]
[Serializable]
public partial class gttDXGridDashboard : UserControl
{
// define a property called "DropZone"
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public PanelControl DropZone
{
get { return panelControlCustom; }
}
// ...
}
public class UserControlDesigner : ParentControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
if (Control is gttDXGridDashboard dashboard)
{
bool result = EnableDesignMode(dashboard.DropZone, "DropZone");
// this writeline never shows, maybe because it's in a control from the toolbox ?
Console.WriteLine($"EnableDesignMode(dashboard.DropZone, "DropZone") = {result}");
}
}
}
Now for the problem: in a form that has a gttDXGridDashboard
on it, and has a button in the dropzone, the designer.cs
looks like this:
// gttDXGridDashboardList.DropZone
//
this.gttDXGridDashboardList.DropZone.Controls.Add(this.gttDXSimpleButtonAddMultipleChassis);
This worked well while the project was running on the .NET framework, but since the move to .NET 8, this line in the designer.cs
is removed from the moment I drop another control on the dropzone, or even when I for example move the existing button (gttDXSimpleButtonAddMultipleChassis
) a bit to the right or left
At that moment, the button gttDXSimpleButtonAddMultipleChassis
still exists on the form, but has no parent anymore and thus is invisible and unusable.
I tried updating all packages, rebuilding all sources, restarted VS, but no results.
Then I wanted to make a workaround, and write some code that will put all the controls without a parent back onto the dropzone, but I don’t know how to search for controls that have no parent, I cannot loop over this.Controls
because these controls have no parent anymore.
So I need a little help here.
Did I do something wrong with defining the dropzone?
Is this a know bug/problem and if so is there a fix/workaround?
If all fails, how can I find controls that have no parent?
I know I can always write code in the forms that have this problem to put the controls back, but that would mean writing almost the same code again in several forms, I want a more generic workaround.