简体   繁体   中英

Add child to custom Control in Designer mode

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" (ie by my code)?

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

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 .

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
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM