none
EnableDesignMode with Inherited UserControl RRS feed

  • Question

  •  

    I am trying to create a UserControl that contains a panel into which additional controls can be added.  I've read through a number of posts in these forums that handle this functionality by specifying a custom control designer class that uses EnableDesignMode in the overridden Initialize method.  This works perfectly fine if I then add the UserControl to the design surface of a WinForm or other UserControl.

     

    However, what I want to do is instead inherit from this UserControl and have the subclass be able to add controls to that same panel.  When I add the new inherited UserControl and select the existing UserControl as the base class the designer does not seem to recognize or respect the custom control designer or the call to EnableDesignMode.  I can see and select the panel but it shows the properties for the private panel, not the property that I had exposed.

     

    Is designing an extended UserControl a supported scenario?

     

    This is what I have:

     

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Drawing;

    using System.Data;

    using System.Text;

    using System.Windows.Forms;

    using System.Windows.Forms.Design;

     

    namespace WindowsApplication1 {

    [Designer(typeof(TestControlBaseDesigner))]

    public class TestControlBase : UserControl {

    private Panel panel;

     

    public TestControlBase() {

    panel = new Panel();

    panel.Dock = DockStyle.Fill;

    Controls.Add(panel);

    Padding = new Padding(10);

    }

     

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

    public Panel OptionsPanel {

    get { return panel; }

    set { panel = value; }

    }

    }

     

    public class TestControlBaseDesigner : ControlDesigner {

    public override void Initialize(IComponent component) {

    base.Initialize(component);

    TestControlBase ctl = (TestControlBase)component;

    EnableDesignMode(ctl.OptionsPanel, "OptionsPanel");

    }

    }

    }

     

     

    Friday, May 2, 2008 2:44 PM

Answers

  •  

    Hi Justin Spindler,

    Based on my understanding, what you want to do is to enable the design feature for the panel in the derived user control, right? If so, you can set the panel as a protected control, then you can add controls to this panel by dragging the control onto the panel.

    Code Snippet
            protected System.Windows.Forms.Panel panel1;

     

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Thursday, May 8, 2008 11:28 AM

All replies

  •  

    Hi Justin Spindler,

    Based on my understanding, what you want to do is to enable the design feature for the panel in the derived user control, right? If so, you can set the panel as a protected control, then you can add controls to this panel by dragging the control onto the panel.

    Code Snippet
            protected System.Windows.Forms.Panel panel1;

     

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Thursday, May 8, 2008 11:28 AM
  •  

    Hi,

     

    I have pretty much the same question but for FlowLayoutPanel and TableLayoutPanel.  Even with the modifiers set to Protected, the derived usercontrol can't modify or add anything in the FlowLayout.  Is there something else that needs to be change to expose the Layout controls in the designer view?

     

    Thanks

    Thursday, May 8, 2008 9:19 PM
  • Hello Birtz,

    I've experienced the same problem. I found out that there's no way to add/edit controls in the TableLayoutPanel created in derived forms or user controls in Visual Designer. The control
    TableLayoutPanel simply doesn't support this and a 'lock' will always appear.

    See the MSDN: http://msdn.microsoft.com/en-us/library/ms171689.aspx

    I suggest you to try design your control without these 'layout' controls if you really intend 'visually' derive them.
    Good luck

    Tuesday, May 20, 2008 1:14 PM
  • I found a semi-solution to this that may be helpful, as I've encountered this same problem and came here looking for a solution, which apparently is not possible.

    • First, make a "Frame" control that contains the Panel that you want to put stuff in, lets call it PanelFrame. Then do what Justin said he tried originally to enable design mode on the Panel.
    • Then you can create a blank control, lets call it PanelParent. This can be where you put all your functions you want to override in different ways in each of the child controls.
    • Then for each child control, inherit from PanelParent and place PanelFrame into the control.

    This way you have several child controls that all can be generalized as type PanelParent, but also contain the common frame that can contain different stuff depending on the child control.

     

    Good luck,

    Evan

    Tuesday, May 20, 2008 1:36 PM