User Control as container at design time

前端 未结 3 487
醉梦人生
醉梦人生 2020-12-05 07:24

I\'m designing a simple expander control.

I\'ve derived from UserControl, drawn inner controls, built, run; all ok.

Since an inner Control is a Panel, I\'d l

相关标签:
3条回答
  • 2020-12-05 07:59

    In addition to the answer above. It is mentioned in the comments, that the user is able to drag the WorkingArea. My fix for that is to include the WorkingArea panel in another panel, setting it to Dock.Fill. To disallow the user to change it back, I have created a class ContentPanel that overrides and hides the Dock property:

    class ContentPanel : Panel
    {
        [Browsable(false)]
        public override DockStyle Dock
        {
            get { return base.Dock; }
            set { base.Dock = DockStyle.Fill; }
        }
    }
    

    For me, this makes it sufficiently safe. We are only using the control internally, so we mainly want to prevent developers from accidently dragging things around. There are certainly ways to mess it up anyway.

    0 讨论(0)
  • 2020-12-05 08:04

    To prevent the working area from being moved/resized in the designer you have to create a class for that working area that hides the Location, Height, Width, Size properties from the designer:

    public class WorkingArea : Panel
    {
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Point Location
        {
            get
            {
                return base.Location;
            }
            set
            {
                base.Location = value;
            }
        }
    ...
    }   
    
    0 讨论(0)
  • 2020-12-05 08:08

    ParentControlDesigner doesn't know what you want do. It only knows you want your UserControl to be a container.

    What you need to do is implement your own designer which enables design mode on the panel:

        using System.ComponentModel;
        using System.Windows.Forms;
        using System.Windows.Forms.Design;
    
        namespace MyCtrlLib
        {
            // specify my custom designer
            [Designer(typeof(MyCtrlLib.UserControlDesigner))]
            public partial class UserControl1 : UserControl
            {
                public UserControl1()
                {
                    InitializeComponent();
                }
    
                // define a property called "DropZone"
                [Category("Appearance")]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
                public Panel DropZone
                {
                    get { return panel1; }
                }
            }
    
            // my designer
            public class UserControlDesigner  : ParentControlDesigner
            {
                public override void Initialize(System.ComponentModel.IComponent component)
                {
                    base.Initialize(component);
    
                    if (this.Control is UserControl1)
                    {
                        this.EnableDesignMode(
                           (UserControl1)this.Control).DropZone, "DropZone");
                    }
                }
            }
        }
    

    I learned this from Henry Minute on CodeProject. See the link for some improvements on the technique.

    0 讨论(0)
提交回复
热议问题