C# 如何在winforms设计器中访问继承的控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1046328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:55:11  来源:igfitidea点击:

How to access inherited controls in the winforms designer

c#winformsvisual-studio-2008

提问by Ksempac

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.

我正在制作一些控件,它们都必须具有相同的外观和一些共同的行为,尽管它们用于不同类型的输入。所以我做了一个继承自 UserControl 的 BaseClass,我所有的控件都继承自 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.

但是,如果我在设计器中为 BaseClass 添加控件,例如 TableLayoutPanel,则在设计继承的类时将无法访问它们。我看到了 TableLayoutPanel,但即使他受到“保护”,我也无法通过设计器修改它或将控件放入其中。我可以通过代码访问它,但我不想失去使用设计器的能力。

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 ?

现在,我只是从 BaseClass 中删除了所有控件,在每个继承的类中添加了布局和所有公共控件,然后使用引用在 BaseClass 中操作它们。但这根本不能让我满意。有没有办法让设计器使用继承的受保护成员控件?

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

环境: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 :

编辑以回答 SLaks 的建议。我尝试设置一个属性,虽然我不习惯使用它们,但它似乎不起作用。这是我试过的代码:

    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();
            }
    }

采纳答案by Ben Arroyo

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".

当您尝试从带有设计器的继承控件访问基本控件中声明的 TableLayoutPanel 时,您正在使用 WinForms 中称为“视觉继承”的功能。

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

不幸的是 TableLayoutPanel 不支持视觉继承:http: //msdn.microsoft.com/en-us/library/ms171689%28VS.80%29.aspx

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

这就是 TableLayoutPanel 在继承的控件中出现阻塞的原因。

回答by Shea

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.

您必须自己设计基本控件。成功重建控件项目后,更改会反映在设计器中。如果您将成员设为公开,您可以对其进行编辑,但更改不会持续存在。

回答by SLaks

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

尝试将此属性添加到面板的定义中(这可能有帮助,也可能无济于事):

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

回答by SLaks

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

尝试ParentControlDesigner为您的控件制作 ,覆盖InternalControlDesigner,并返回(designerHost.GetDesigner(tableLayoutPanel) as ControlDesigner)designerHost(IDesignerHost) component.Site.GetService(typeof(IDesignerHost))

回答by serialhobbyist

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.

我依稀记得通过将基类放在自己的 DLL 中并首先构建它来解决类似的问题。我翻了翻,但找不到项目。对不起。