0

I have already seen this answer for adding designer support to a custom Control. It works up to and including adding child Controls. Except that after closing the designer and opening it again - those children are gone.

Is there some way to get support for adding child Controls in the designer? Perhaps by some event that we can handle somehow "manually" (i.e. by my code)?

This is not a UserControl. It's a class inheriting from Panel.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Adding child controls in which designer? How? What exactly happens? – SLaks May 23 '17 at 20:53
  • @SLaks You know the Winforms design mode, right? The one that gave Visual Studio its name. I want to drag a Control from the **Toolbox** and drop it on my `customControl1`'s (that is already on the `Form`) `panel1`, and have that new child Control added to `panel1`'s `Controls` property. The child is seen in the designer and in **Document outline**, but after I close the designer and reopen it - the child is gone. – ispiro May 23 '17 at 20:56
  • You mean the form's designer, not the control's own designer. – SLaks May 23 '17 at 21:01
  • @SLaks The Control does not have a designer. This is _not_ a user control. It's a custom control. – ispiro May 23 '17 at 21:03

2 Answers2

0

You need to shadow or override the Controls property and apply [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)].

This will tell the form's designer to save the contents of that property to its .Designer.cs.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Any idea how I would do that? It's a custom control - one that inherits from Panel. It's not a user control. – ispiro May 23 '17 at 21:05
0

For a class derived from Panel, the designer is enabled by default and you don't need to do anything, because the designer of panel is PanelDesigner which is derived from ScrollableControlDeigner which is derived from ParentControlDesigner:

ParentControlDesigner provides a base class for designers of controls that can contain child controls. In addition to the methods and functionality inherited from the ControlDesigner and ComponentDesigner classes, ParentControlDesigner enables child controls to be added to, removed from, selected within, and arranged within the control whose behavior it extends at design time.

You can enable the designer by setting ParentControldesigner as designer of your control. For example:

using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[Designer(typeof(ParentControlDesigner))]
public class MyControl : Control
{
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398