2017-03-13 3 views
1

非常に厄介な問題があります。C#でユーザーコントロールライブラリを更新すると内部コンテンツが削除されます

私のユーザーコントロールライブラリには、内部にパネルを持つコントロールがあり、コンテンツを取ることができます。 このDLLをビルドしてプロジェクトに参照する場合は、通常、このコントロールを使用してコンテンツを追加することができます。 問題:毎回このコントロールライブラリの新しいバージョンをビルドし、プロジェクトのdllを更新/上書きします。このDLLを使用すると、コンテンツのコントロールが再構築され、コンテンツが削除されます。 これを修正するには?


敬具

スヴェンケーニッヒ

EDIT 1つの
注:私は意味パネルは、コンテンツを格納するためのWindows.Forms.Panelを持っているユーザーコントロールです。 このユーザーコントロールのデザイナーは「ParentControlDesigner」です。

EDIT 2
申し訳ありませんが、コンテンツを保存するための制御は、直接ParentControlDesignerによって設計されたユーザーコントロールに保存されます。 私はWindows.Forms.Panelを使用するとデザイン時にサイズ変更オプションがあるので、これを実行しました。そして、私はこれが嫌いです。ここで

EDIT 3
コードあり

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

そしてここでは、 "パネル" である...コンテンツコントロールが内部でのGroupBox、です...

[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; 
} 
+0

どのようにしてパネルの内容を保存している:

ここでユーザーコントロールの新しいコードはありますか?コードによっては、これが本当に簡単に私の友人になります。 – Trey

+0

「パネルのコンテンツを保存する」とはどういう意味ですか? –

+0

あなたのコードを投稿してください、私はあなたが問題を抱えているものを少し失っています、そして、私はあなたのユーザコントロールコードがこれを照らしているのを見ていると思います。 – Trey

答えて

0

ここに解決策があります。

問題は、コントロールを子ユーザーコントロールに追加することでした。そのため、解決策は、コントロールをユーザーコントロールに直接追加し、OnControlAddedイベントをオーバーライドして追加されたコントロールを前面に表示することです。

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

     e.Control.BringToFront(); 
    } 

私はデザイン時にコントロールを追加する場合、設計者は正確に限り、私が構築したり、再構築していないとして追加されたコントロール を示したので。
現時点では、フォームを構築/再構築するときに、デザイナはユーザーコントロールにコントロールを追加するよう指示します。

ユーザーコントロールはコントロールを正しく追加しましたが、基本コントロールを含むContainerControlの下にあります。 =>コントロールが見えなくなりました。

[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 
    } 
} 
0

ここに私のコードの更新です。

変更点:
- 標準のWindows.Forms.Panelをコントロールコンテナとして使用するようになりました。
- 私は、「サイズを変更」し、その結果の「LocationChanged」ここ


絵イベントへの書き込みのコードをしました、サイズ変更や移動を避けるために:
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 
    } 
} 
+0

申し訳ありませんが、あまりにも多くの時間をこの幸運に費やしました。私の最後に機能するコードを取得できません。動的パネルの作成は、あなたの問題がどこにあるのか疑いがあります。 – Trey

+0

あなたの時間のために私の友人をありがとう:) –

+0

私は余裕があれば問題はありません...とにかく幸運な友達。 – Trey

関連する問題