C#更新usercontrol库将删除内部内容

时间:2017-03-13 18:36:20

标签: c# user-controls panel rebuild

我有一个非常讨厌的问题!

在我的用户控件库中,我有一个控件,里面有一个面板,可以获取内容。 如果我构建这个DLL并将其引用到项目中,我通常可以使用此控件并向其添加内容。 现在的问题是:每次我构建这个控件库的新版本并更新/覆盖项目中的dll我都会使用这个dll来重建控件,现在内容将被删除。 如何解决这个问题?


最好的问候

SvenKönig

编辑1
注意:我所说的面板是一个用户控件,它有一个Windows.Forms.Panel来存储内容。 该用户控件的设计者是“ParentControlDesigner”。

编辑2
抱歉,保存到内容的控件直接保存到通过ParentControlDesigner设计的用户控件。 我这样做是因为如果我使用Windows.Forms.Panel我在设计时调整了选项。而这我不想要。

编辑3
这是GroupBox的代码,其中内容控件位于...

[Designer(typeof(BorderedGroupBoxDesigner))]
public partial class BorderedGroupBox : UserControl
{
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    internal Panel GroupingPanel { get; }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    internal readonly Label TitelLabel;

    private VisualStyleRenderer _renderer;

    /// <summary>
    /// Initializes the Control.
    /// </summary>
    public BorderedGroupBox()
    {
        InitializeComponent();

        //Create TitleLabel
        TitelLabel = new Label
        {
            Location = new Point(1, 1),
            AutoSize = false,
            TextAlign = ContentAlignment.MiddleLeft
        };

        //Create GroupingPanel
        GroupingPanel = new Panel
        {
            Location = new Point(1, TitleHeight - 1)
        };

        //Create Container and add Panel
        Control container = new ContainerControl
        {
            Dock = DockStyle.Fill,
            Padding = new Padding(-1),
            Controls = {TitelLabel, GroupingPanel}
        };

        //Add container and it's content to this control
        Controls.Add(container);

        //Set sizes of inner controls
        TitelLabel.Size = new Size(Size.Width - 2, 20);
        GroupingPanel.Size = new Size(Size.Width - 2, Size.Height - TitleHeight - 3);

        //Set anchor of inner controls
        TitelLabel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        GroupingPanel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

        //Value defaults
        BackgroundColor = SystemColors.Window;
        BorderColor = SystemColors.GradientActiveCaption;
        TitleBackColor = Color.FromKnownColor(KnownColor.DodgerBlue);
        TitleFont = new Font("Calibri", TitleHeight - 9, FontStyle.Bold);
        TitleFontColor = SystemColors.Window;
    }

    //Make default prope rty "BackColor" unvisible
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public sealed override Color BackColor { get; set; }

    //Use "BackgroundColor" instead of default "BackColor"
    /// <returns>The BackgroundColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("The backgroundcolor of the component.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BackgroundColor { get { return GroupingPanel.BackColor; } set { GroupingPanel.BackColor = value; } }

    /// <returns>The BorderColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the border color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "GradientActiveCaption")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BorderColor { get { return BackColor; } set { BackColor = value; } }

    /// <returns>The BorderColor of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "DodgerBlue")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleBackColor { get { return TitelLabel.BackColor; } set { TitelLabel.BackColor = value; } }

    /// <returns>The height of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title height in px.")]
    [Category("Appearance")]
    [DefaultValue(typeof(int), "20")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public int TitleHeight
    {
        get { return TitelLabel.Size.Height; }
        set
        {
            TitelLabel.Size = new Size(TitelLabel.Size.Width, value);
            GroupingPanel.Location = new Point(GroupingPanel.Location.X, value + 2);
            GroupingPanel.Size = new Size(GroupingPanel.Size.Width, Size.Height - value - 3);
        }
    }

    /// <returns>The font of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Font), "Calibri; 11pt; style=Bold")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Font TitleFont { get { return TitelLabel.Font; } set { TitelLabel.Font = value; } }

    /// <returns>The ForeColor (color of the text) of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleFontColor { get { return TitelLabel.ForeColor; } set { TitelLabel.ForeColor = value; } }

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title text.")]
    [Category("Appearance")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override string Text { get { return TitelLabel.Text; } set { TitelLabel.Text = value; } }

    /// <returns>Sets visibility of the design grid to easily align controls on grid.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets visibility of the design grid to easily align controls on grid.")]
    [Category("Design")]
    [DesignOnly(true)]
    [DefaultValue(typeof(bool), "false")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public bool ShowDesignGrid
    {
        get { return GroupingPanel.Designer.DrawGridState; }
        set
        {
            if (value)
                GroupingPanel.Designer.EnableDrawGrid();
            else
                GroupingPanel.Designer.DisableDrawGrid();

            Refresh();
        }
    }

    /// <returns>Sets visibility of the design grid to easily align controls on grid.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets size of the design grid.")]
    [Category("Design")]
    [DesignOnly(true)]
    [DefaultValue(typeof(Size), "8; 8")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string DesignGridSize
    {
        get { return $"{GroupingPanel.Designer.GridSize.Width}; {GroupingPanel.Designer.GridSize.Height}"; }
        set
        {
            var values = value.Split(';');

            GroupingPanel.Designer.GridSize = new Size(int.Parse(values[0]), int.Parse(values[1]));
        }
    }

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public new Padding Padding
    {
        get { return GroupingPanel.Padding; }
        set { GroupingPanel.Padding = value; }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (!Focused || !Application.RenderWithVisualStyles) return;

        if (_renderer == null)
        {
            var elem = VisualStyleElement.Button.PushButton.Normal;
            _renderer = new VisualStyleRenderer(elem.ClassName, elem.Part, (int)PushButtonState.Normal);
        }

        var rc = _renderer.GetBackgroundContentRectangle(e.Graphics, new Rectangle(0, 0, Width, Height));
        rc.Height--;
        rc.Width--;

        using (var p = new Pen(Brushes.Purple))
        {
            e.Graphics.DrawRectangle(p, rc);
        }
    }
}

internal class BorderedGroupBoxDesigner : ControlDesigner
{
    internal static SelectionRulesEnum SelectionRule;

    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        EnableDragDrop(true);

        var uc = component as BorderedGroupBox;
        if (uc != null)
            EnableDesignMode(uc.GroupingPanel, "Panel");
    }

    public override SelectionRules SelectionRules
    {
        get
        {
            switch (SelectionRule)
            {
                case SelectionRulesEnum.All:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.AllSizeable;
                case SelectionRulesEnum.UpDown:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.TopSizeable | SelectionRules.BottomSizeable;
                case SelectionRulesEnum.RightLeft:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable;
                case SelectionRulesEnum.None:
                    return SelectionRules.Visible | SelectionRules.Moveable;
                default:
                    return SelectionRules.Visible | SelectionRules.Moveable;
            }
        }
    }

    internal enum SelectionRulesEnum
    {
        All,
        UpDown,
        RightLeft,
        None
    }
}

这是“小组”......

[Designer(typeof(NonSizeablePanel_ParentDesigner))]
internal partial class Panel : UserControl
{
    internal NonSizeablePanel_ParentDesigner Designer;

    internal Panel()
    {
        InitializeComponent();
    }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new Point Location { get { return base.Location; } set { base.Location = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new AnchorStyles Anchor { get { return base.Anchor; } set { base.Anchor = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new Size Size { get { return base.Size; } set { base.Size = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new bool AutoScroll { get { return base.AutoScroll; } set { base.AutoScroll = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new Size AutoScrollMargin { get { return base.AutoScrollMargin; } set { base.AutoScrollMargin = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new Size AutoScrollMinSize { get { return base.AutoScrollMinSize; } set { base.AutoScrollMinSize = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new bool AutoSize { get { return base.AutoSize; } set { base.AutoSize = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new DockStyle Dock { get { return base.Dock; } set { base.Dock = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new Padding Margin { get { return base.Margin; } set { base.Margin = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new Padding Padding { get { return base.Padding; } set { base.Padding = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new Size MaximumSize { get { return base.MaximumSize; } set { base.MaximumSize = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new Size MinimumSize { get { return base.MinimumSize; } set { base.MinimumSize = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new bool Visible { get { return base.Visible; } set { base.Visible = value; } }

    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public new bool Enabled { get { return base.Enabled; } set { base.Enabled = value; } }
}

internal class NonSizeablePanel_ParentDesigner : ParentControlDesigner
{
    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        var userControl = component as Panel;

        if (userControl != null)
            userControl.Designer = this;
    }

    internal new Size GridSize
    {
        get { return base.GridSize; }
        set { base.GridSize = value; }
    }

    internal bool DrawGridState => DrawGrid;

    internal void EnableDrawGrid()
    {
        DrawGrid = true;
    }

    internal void DisableDrawGrid()
    {
        DrawGrid = false;
    }

    protected override bool DrawGrid { get; set; }

    //Disable any sizing grips
    public override SelectionRules SelectionRules => SelectionRules.None;
}

2 个答案:

答案 0 :(得分:0)

这是我的代码的更新。

变化:
- 现在使用标准的Windows.Forms.Panel作为控件容器。
- 为了避免调整大小或移动我已经为事件编写了代码&#34; Resize&#34;和&#34; LocationChanged&#34;


这里是结果的图片:
Current result


这里是当前的代码:

[Designer(typeof(BorderedGroupBoxDesigner))]
public sealed partial class BorderedGroupBox : UserControl
{
    /// <summary>The Panel which stores the content controls.</summary>
    [Category("Behavior")]
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public readonly Panel ContentPanel;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    internal readonly Label TitelLabel;

    private VisualStyleRenderer _renderer;

    private int _contentPanelTop => TitleHeight - 1;

    private int _contentPanelLeft => 1;

    private int _contentPanelWidth => Size.Width - 2;

    private int _contentPanelHeight => Size.Height - TitleHeight - 3;

    /// <summary>
    /// Initializes the Control.
    /// </summary>
    public BorderedGroupBox()
    {
        InitializeComponent();

        //Create TitleLabel
        TitelLabel = new Label
        {
            Location = new Point(1, 1),
            AutoSize = false,
            TextAlign = ContentAlignment.MiddleLeft
        };

        //Create GroupingPanel
        ContentPanel = new Panel
        {
            Location = new Point(1, TitleHeight - 1)
        };

        //Create Container and add Panel
        Control container = new ContainerControl
        {
            Dock = DockStyle.Fill,
            Padding = new Padding(-1),
            Controls = {TitelLabel, ContentPanel}
        };

        //Add container and it's content to this control
        Controls.Add(container);

        //Set sizes of inner controls
        TitelLabel.Size = new Size(Size.Width - 2, 20);
        ContentPanel.Size = new Size(Size.Width - 2, Size.Height - TitleHeight - 3);

        //Set anchor of inner controls
        TitelLabel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        ContentPanel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

        //Value defaults
        BackgroundColor = SystemColors.Window;
        BorderColor = SystemColors.GradientActiveCaption;
        TitleBackColor = Color.FromKnownColor(KnownColor.DodgerBlue);
        TitleFont = new Font("Calibri", TitleHeight - 9, FontStyle.Bold);
        TitleFontColor = SystemColors.Window;
        AllowDrop = true;

        //Set event handler
        ContentPanel.Resize += ContentPanelOnResize;
        ContentPanel.LocationChanged += ContentPanelOnLocationChanged;
    }

    private void ContentPanelOnLocationChanged(object sender, EventArgs eventArgs)
    {
        if (ContentPanel.Left != _contentPanelLeft | ContentPanel.Top != _contentPanelTop)
            ContentPanel.Location = new Point(_contentPanelLeft, _contentPanelTop);
    }

    private void ContentPanelOnResize(object sender, EventArgs eventArgs)
    {
        if (ContentPanel.Size.Width != _contentPanelWidth | ContentPanel.Size.Height != Size.Height - TitleHeight - 3)
            ContentPanel.Size = new Size(_contentPanelWidth, Size.Height - TitleHeight - 3);
    }

    //Make default property "BackColor" unvisible
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override Color BackColor { get; set; }

    //Use "BackgroundColor" instead of default "BackColor"
    /// <returns>The BackgroundColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("The backgroundcolor of the component.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BackgroundColor { get { return ContentPanel.BackColor; } set { ContentPanel.BackColor = value; } }

    /// <returns>The BorderColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the border color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "GradientActiveCaption")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BorderColor { get { return BackColor; } set { BackColor = value; } }

    /// <returns>The BorderColor of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "DodgerBlue")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleBackColor { get { return TitelLabel.BackColor; } set { TitelLabel.BackColor = value; } }

    /// <returns>The height of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title height in px.")]
    [Category("Appearance")]
    [DefaultValue(typeof(int), "20")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public int TitleHeight
    {
        get { return TitelLabel.Size.Height; }
        set
        {
            TitelLabel.Size = new Size(TitelLabel.Size.Width, value);
            ContentPanel.Location = new Point(ContentPanel.Location.X, value + 2);
            ContentPanel.Size = new Size(ContentPanel.Size.Width, Size.Height - value - 3);
        }
    }

    /// <returns>The font of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Font), "Calibri; 11pt; style=Bold")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Font TitleFont { get { return TitelLabel.Font; } set { TitelLabel.Font = value; } }

    /// <returns>The ForeColor (color of the text) of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleFontColor { get { return TitelLabel.ForeColor; } set { TitelLabel.ForeColor = value; } }

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title text.")]
    [Category("Appearance")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override string Text { get { return TitelLabel.Text; } set { TitelLabel.Text = value; } }

    /// <returns>Sets the interior spacing in the control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the interior spacing in the control.")]
    [Category("Layout")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public new Padding Padding
    {
        get { return ContentPanel.Padding; }
        set { ContentPanel.Padding = value; }
    }

    /// <summary>Raises the <see cref="E:System.Windows.Forms.Control.Paint" /> event.</summary>
    /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data. </param>
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (!Focused || !Application.RenderWithVisualStyles) return;

        if (_renderer == null)
        {
            var elem = VisualStyleElement.Button.PushButton.Normal;
            _renderer = new VisualStyleRenderer(elem.ClassName, elem.Part, (int)PushButtonState.Normal);
        }

        var rc = _renderer.GetBackgroundContentRectangle(e.Graphics, new Rectangle(0, 0, Width, Height));
        rc.Height--;
        rc.Width--;

        using (var p = new Pen(Brushes.Purple))
        {
            e.Graphics.DrawRectangle(p, rc);
        }
    }
}

internal class BorderedGroupBoxDesigner : ControlDesigner
{
    private BorderedGroupBox borderedGroupBox;

    internal static SelectionRulesEnum SelectionRule;

    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        EnableDragDrop(true);

        borderedGroupBox = component as BorderedGroupBox;

        if (borderedGroupBox != null)
            EnableDesignMode(borderedGroupBox.ContentPanel, "Panel");
    }

    public override SelectionRules SelectionRules
    {
        get
        {
            switch (SelectionRule)
            {
                case SelectionRulesEnum.All:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.AllSizeable;
                case SelectionRulesEnum.UpDown:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.TopSizeable | SelectionRules.BottomSizeable;
                case SelectionRulesEnum.RightLeft:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable;
                case SelectionRulesEnum.None:
                    return SelectionRules.Visible | SelectionRules.Moveable;
                default:
                    return SelectionRules.Visible | SelectionRules.Moveable;
            }
        }
    }

    internal enum SelectionRulesEnum
    {
        All,
        UpDown,
        RightLeft,
        None
    }
}

答案 1 :(得分:0)

这是解决方案。

问题是将控件添加到子用户控件。因此,解决方案是将控件直接添加到用户控件中并覆盖OnControlAdded事件,以便在前面添加控件。

    protected override void OnControlAdded(ControlEventArgs e)
    {
        base.OnControlAdded(e);

        e.Control.BringToFront();
    }

因为如果我在设计时添加控件,设计师会显示添加的控件 只要我不构建或重建,就是正确的。
目前,我构建/重建表单,设计师告诉用户控件添加一个控件。

用户控件正确添加了控件,但在包含基本控件的ContainerControl下。 =&GT;控制不再可见。

以下是用户控件的新代码:

[Designer(typeof(BorderedGroupBoxDesigner))]
[DefaultEvent("Load")]
public sealed partial class BorderedGroupBox : UserControl
{
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    internal readonly Panel BackgroundPanel;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    internal readonly Label TitelLabel;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    internal new readonly ContainerControl Container;

    private VisualStyleRenderer _renderer;

    private int _contentPanelTop => TitleHeight + 2;

    private int _contentPanelLeft => 1;

    private int _contentPanelWidth => Size.Width - 2;

    private int _contentPanelHeight => Size.Height - TitleHeight;

    /// <summary>
    /// Initializes the Control.
    /// </summary>
    public BorderedGroupBox()
    {
        InitializeComponent();

        //Create TitleLabel
        if (TitelLabel == null)
            TitelLabel = new Label
            {
                Location = new Point(1, 1),
                AutoSize = false,
                TextAlign = ContentAlignment.MiddleLeft
            };

        //Create BackgroundPanel
        if (BackgroundPanel == null)
            BackgroundPanel = new Panel
            {
                Location = new Point(1, TitleHeight - 1)
            };

        //Create Container and add Panel
        if (Container == null)
            Container = new ContainerControl
            {
                Dock = DockStyle.Fill,
                Padding = new Padding(-1),
                Controls = { TitelLabel, BackgroundPanel }
            };

        //Add container and it's content to this control
        Controls.Add(Container);

        //Set sizes of inner controls
        TitelLabel.Size = new Size(Size.Width - 2, 20);
        BackgroundPanel.Size = new Size(Size.Width - 2, Size.Height - TitleHeight - 3);

        //Set anchor of inner controls
        TitelLabel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        BackgroundPanel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

        //Value defaults
        BackgroundColor = SystemColors.Window;
        BorderColor = SystemColors.GradientActiveCaption;
        TitleBackColor = Color.FromKnownColor(KnownColor.DodgerBlue);
        TitleFont = new Font("Calibri", TitleHeight - 9, FontStyle.Bold);
        TitleFontColor = SystemColors.Window;
        AllowDrop = true;
    }

    //Make default property "BackColor" unvisible
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override Color BackColor { get; set; }

    //Use "BackgroundColor" instead of default "BackColor"
    /// <returns>The BackgroundColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("The backgroundcolor of the component.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BackgroundColor { get { return BackgroundPanel.BackColor; } set { BackgroundPanel.BackColor = value; } }

    /// <returns>The BorderColor associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the border color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "GradientActiveCaption")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color BorderColor { get { return BackColor; } set { BackColor = value; } }

    /// <returns>The BorderColor of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "DodgerBlue")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleBackColor { get { return TitelLabel.BackColor; } set { TitelLabel.BackColor = value; } }

    /// <returns>The height of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title height in px.")]
    [Category("Appearance")]
    [DefaultValue(typeof(int), "20")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public int TitleHeight
    {
        get { return TitelLabel.Size.Height; }
        set
        {
            TitelLabel.Size = new Size(TitelLabel.Size.Width, value);
            BackgroundPanel.Location = new Point(BackgroundPanel.Location.X, value + 2);
            BackgroundPanel.Size = new Size(BackgroundPanel.Size.Width, Size.Height - value - 3);
        }
    }

    /// <returns>The font of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Font), "Calibri; 11pt; style=Bold")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Font TitleFont { get { return TitelLabel.Font; } set { TitelLabel.Font = value; } }

    /// <returns>The ForeColor (color of the text) of the title associated with this control.</returns>
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title font color.")]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "Window")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public Color TitleFontColor { get { return TitelLabel.ForeColor; } set { TitelLabel.ForeColor = value; } }

    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Sets the title text.")]
    [Category("Appearance")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public override string Text { get { return TitelLabel.Text; } set { TitelLabel.Text = value; } }

    /// <summary>Raises the <see cref="E:System.Windows.Forms.Control.Paint" /> event.</summary>
    /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs" /> that contains the event data. </param>
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (!Focused || !Application.RenderWithVisualStyles) return;

        if (_renderer == null)
        {
            var elem = VisualStyleElement.Button.PushButton.Normal;
            _renderer = new VisualStyleRenderer(elem.ClassName, elem.Part, (int)PushButtonState.Normal);
        }

        var rc = _renderer.GetBackgroundContentRectangle(e.Graphics, new Rectangle(0, 0, Width, Height));
        rc.Height--;
        rc.Width--;

        using (var p = new Pen(Brushes.Purple))
        {
            e.Graphics.DrawRectangle(p, rc);
        }
    }

    /// <summary>Raises the <see cref="E:System.Windows.Forms.Control.ControlAdded" /> event.</summary>
    /// <param name="e">A <see cref="T:System.Windows.Forms.ControlEventArgs" /> that contains the event data. </param>
    protected override void OnControlAdded(ControlEventArgs e)
    {
        base.OnControlAdded(e);

        e.Control.BringToFront();
    }
}

internal class BorderedGroupBoxDesigner : ParentControlDesigner
{
    internal static SelectionRulesEnum SelectionRule;

    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        EnableDragDrop(true);
    }

    public override SelectionRules SelectionRules
    {
        get
        {
            switch (SelectionRule)
            {
                case SelectionRulesEnum.All:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.AllSizeable;
                case SelectionRulesEnum.UpDown:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.TopSizeable | SelectionRules.BottomSizeable;
                case SelectionRulesEnum.RightLeft:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable;
                case SelectionRulesEnum.None:
                    return SelectionRules.Visible | SelectionRules.Moveable;
                default:
                    return SelectionRules.Visible | SelectionRules.Moveable;
            }
        }
    }

    internal enum SelectionRulesEnum
    {
        All,
        UpDown,
        RightLeft,
        None
    }
}