views:

125

answers:

2

Hi,

I want to create my own component which consists two other panels. One of them has fixed contents (such as control buttons, etc.) and the other is standard panel, where I can add other components in designer (VS2008). I know that I have to create UserControl, where I can place my two panels. Then I want to insert my component into the form. But I don't know how to create behavior where I can add other components (such as buttons, labels, etc.) only into second panel in my component. Could anyone help me with creating this component?

Thank you. Adam.

A: 

Here is an example (snippet of working code):

  [Designer(typeof(NavigationalUserControl.Designer))]
  public partial class NavigationalUserControl : UserControl
  {
    class Designer : ControlDesigner 
    {
      public override void Initialize(IComponent component)
      {
        base.Initialize(component);
        var nc = component as NavigationalUserControl;
        EnableDesignMode(nc.panel2, "ContainerPanel"); 
        EnableDesignMode(nc.bottomPanel, "BottomPanel");
      }
    }

    // rest of normal class
  }
leppie
That's it. Big thanks for your help
Adam
There is another problem in this solution.Component behavior in designer mode is correct, but whenever I add any component into the container panel and run the program, the component disappears. It seems that designer doesn't add component in the InitComponent section. Is there any way to fix it? Thank you.
Adam
@Adam: Just add it in the constructor. Having it in InitComponent section might cause it to be lost.
leppie
It means that if I want to add button into the container panel, I will have to manually add into the constructor something like this : panel2.Controls.Add(button1);?Isn't there any better way? I think that I'm doing something wrong. :-(
Adam
@Adam: No, that will be added in InitComponent then from the designer. If it is permanent, then add it in the constructor.
leppie
A: 

I have found the correct solution (I hope). I have added into my UserControl a property which returns the content panel with this specific Attribute:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Panel PanelContent
{
   get { return this.panel2; }
}

Thanks for your help leppie

Adam