views:

1566

answers:

2

Hello,

I create my own user control which only contains a panel :

When I drag a myPanel object in the designer and then try to add a button on it, the button is in fact added to the form's controls.

Is there a property/attribute I must set in order to perform this, an other way to do what I want ?

public class MyPanel : UserControl
{
    private Panel panelMain;

    public MyPanel()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.panelMain = new System.Windows.Forms.Panel();
        this.SuspendLayout();
        // 
        // panelMain
        // 
        this.panelMain.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.panelMain.Dock = System.Windows.Forms.DockStyle.Fill;
        this.panelMain.Location = new System.Drawing.Point(0, 0);
        this.panelMain.Name = "panelMain";
        this.panelMain.Size = new System.Drawing.Size(150, 150);
        this.panelMain.TabIndex = 0;
        // 
        // myPanel
        // 
        this.Controls.Add(this.panelMain);
        this.Name = "MyPanel";
        this.ResumeLayout(false);
    }
}
+3  A: 

Have a look here

How to make a UserControl object acts as a control container design-time by using Visual C#

But if you only need extended Panel functionality its better to inherit directly from Panel.

gsharp
It is what I what looking for . About inheritancy, I just made the problem simpler and of course I needed something more complicated.
Toto
A: 

To be more precise, I do the following :

[Designer(typeof(myControlDesigner))] 
public class MyPanel : UserControl
{
    private TableLayoutPanel tableLayoutPanel1;
    private Panel panelMain;
    ...

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Panel InnerPanel
    {
        get { return panelMain; }
        set { panelMain = value; }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public TableLayoutPanel InnerTableLayout
    {
        get { return tableLayoutPanel1; }
        set { tableLayoutPanel1 = value; }
    }
}

With myControlDesigner for [Designer(typeof(myControlDesigner))] to be

class myControlDesigner : ParentControlDesigner
{

    public override void Initialize(IComponent component)
    {

        base.Initialize(component);

        MyPanel myPanel = component as MyPanel;
        this.EnableDesignMode(myPanel.InnerPanel, "InnerPanel");
        this.EnableDesignMode(myPanel.InnerTableLayout, "InnerTableLayout");

    }

}

(Other source : Add Control to UserControl )

Toto
I have used your approach for creating a custom panel. But, when I use this custom panel in another user control, when I drag controls on the panel in the designer, they initially appear. But, when I recompile, they disappear. They are still there but it seems the custom panel is draw over/after them. Did you have this sort of problem? I am using VS 2005/.NET 2.0.
bsh152s
No I don't remember having this kind of issues, do you have a small sample ?
Toto