10

I'm making some controls which all have to share the same look and some common behavior, although they are meant for different kind of inputs. So I made a BaseClass which inherit from UserControl, and all my controls inherit from BaseClass.

However, if i add controls for BaseClass in the designer, such as a TableLayoutPanel, i can't access them when I'm designing the inherited classes. I see the TableLayoutPanel, but even though he is "protected", i can't modify it or put controls in it through the designer. I've no trouble accesing it by code, but i don't want to lose the ability to use the designer.

Right now, i simply removed all controls from BaseClass, added the layout and all the common controls in each of the inherited class, then use references to manipulate them inside BaseClass. But that doesn't satisfy me at all. Is there a way to make the designer work with inherited protected member controls ?

Environment : C#, .NET 3.5, Visual Studio 2008

EDIT to answer SLaks's suggestion. I tried setting a property, and although I'm not used to use them it doesn't seem to work. Here is the code i tried :

    public partial class UserControl1 : UserControl
    {
            public UserControl1()
            {
                    InitializeComponent();
            }

            public TableLayoutPanel TableLayoutPanel1
            {
                    get { return tableLayoutPanel1;}
                    set { tableLayoutPanel1 = value;}
            }
    }

    public partial class UserControl2 : UserControl1
    {
            public UserControl2()
            {
                    InitializeComponent();
            }
    }
Ksempac
  • 1,842
  • 2
  • 18
  • 23
  • Would you be able to inherit from TableLayoutPanel itself? – SLaks Jun 26 '09 at 13:17
  • Sure, you can always create your own control inherited from winforms control. I did this to implement some controls. But that's not what I'm trying to achieve here. I'm trying to have a control with several basic controls inside : a tablelayoutpanel, with some controls in it (such as a label), and several empty spaces that inherited classes should fill with the controls of their choice. The end goal is to add all theses controls to the toolbox and then drap and drop them into the designer to create forms who have many controls but common behavior/layout accross theses controls. – Ksempac Jun 26 '09 at 13:41
  • And btw, inheriting from a Winform control will cut out the designer, since usually, the point is only to implement a specific behavior, not redesigning the control yourself. – Ksempac Jun 26 '09 at 13:46

5 Answers5

11

When you try to access from the inherited control with the designer to the TableLayoutPanel declared in the base control, you're using a feature in WinForms called "Visual Inheritance".

Unfortunately TableLayoutPanel doesn't support visual inheritance: http://msdn.microsoft.com/en-us/library/ms171689%28VS.80%29.aspx

That's why the TableLayoutPanel appears blocked in the inherited controls.

Ben Arroyo
  • 351
  • 2
  • 10
  • I ran straight into this problem. FTA: "Use the TableLayoutPanel control sparingly." – Jacobs Data Solutions Mar 21 '12 at 18:51
  • Yes this look like a dead end. I was able to find most of the C# source for tablelayout and the designer but it looks like I will never find the FlowPanelDesigner.uex files associated with this control – garaber Nov 14 '13 at 17:44
1

Try adding this attribute to the definition of the panel (this may or may not help):

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

You have to design the base controls on their own. Changes are reflected in the designer after you successfully rebuild the controls project. If you make the members public you can edit them but the changes won't persist.

Shea
  • 11,085
  • 2
  • 19
  • 21
  • Setting the rows, columns and size of a TableLayoutPanel in the base class is what i would like to do. But it's pointless if i can't add controls in the layout when I'm designing the derived class. For example having a blank cell in my tablelayout, and then add, through the designer, a different control in each of the derived class in this cell. – Ksempac Jun 25 '09 at 21:37
  • You can't modify the base control while designing the derived control. Not easily anyway. If you made the "empty spaces" public (e.g., EmptyPanel1, EmptyPanel2, ...) then provided your own TypeEditor for them you can at least allow them to set the controls visually. Creating a type editor is a bit of work though. – Shea Jun 26 '09 at 21:24
0

Try making a ParentControlDesigner for your control, overriding InternalControlDesigner, and returning (designerHost.GetDesigner(tableLayoutPanel) as ControlDesigner). designerHost is (IDesignerHost) component.Site.GetService(typeof(IDesignerHost)).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

I vaguely remember solving a similar problem by putting the base class it its own DLL and building it first. I've had a rummage but I can't find the project. Sorry.

serialhobbyist
  • 4,768
  • 5
  • 43
  • 65