简体   繁体   English

WinForms ToolStrip 在视觉继承中不可用

[英]WinForms ToolStrip not available in visual inheritence

Lets say I have FormChild inheriting FormParent.可以说我有 FormChild 继承 FormParent。 In parent, there is a toolstip at the top.在父级中,顶部有一个工具提示。 What I want is in my child form to add extra elements, but the control is always locked, although I've set the modifiers to protected .我想要的是在我的子窗体中添加额外的元素,但是控件始终被锁定,尽管我已将修饰符设置为protected

I've checked the Internet, it seems this is a well-known bug;上网查了一下,好像是个众所周知的bug; but does anyone know a workaround or something?但是有人知道解决方法吗?

Well you can manually edit FormChild.Designer.cs file.那么您可以手动编辑 FormChild.Designer.cs 文件。 There you can access inherited toolstrip.在那里您可以访问继承的工具条。

Edit:编辑:

  1. Add this class:添加此 class:

     //System.Design.dll required [Designer(typeof(System.Windows.Forms.Design.ControlDesigner))] public class InheritableToolStrip: ToolStrip { }
  2. Compile project编译项目

  3. Use InheritableToolStrip from toolbox to add to FormParent.使用工具箱中的 InheritableToolStrip 添加到 FormParent。
  4. Change Modifier property to protected.将修饰符属性更改为受保护。
  5. Add items to inherited toolstrip in FormChild.将项目添加到 FormChild 中的继承工具条。

and you should be able to design control in designer.你应该能够在设计器中设计控件。 Tried a bit - seems like it works.尝试了一下 - 似乎它有效。

Ps Iam using VS2010, .NET v4 Ps Iam 使用 VS2010、.NET v4

Microsoft have locked down Visual Inheritance for complex controls because they couldn't figure out how to manage the designer-serialization of child item controls over an inheritance tree in a simple way. Microsoft 已锁定 Visual Inheritance 用于复杂控件,因为他们无法弄清楚如何以简单的方式管理 inheritance 树上的子项控件的设计器序列化。

A simple workaround (which achieves the same effect) is to drop an invisible toolstrip on the sub-form and merge it with the base-forms toolstrip at load time.一个简单的解决方法(达到相同的效果)是在子窗体上放置一个不可见的工具条,并在加载时将其与基本窗体工具条合并。

I've developed MergableToolStrip controls you can use free of charge here 我开发了 MergableToolStrip 控件,您可以在这里免费使用

You can pass the toolstrip to the child form like this.您可以像这样将工具条传递给子窗体。

public partial class ChildForm : Form
{ 
    private readonly ToolStrip parentToolStrip;

    public ChildForm(ToolStrip strip)
    {
        parentToolStrip = strip;
    }
}

Please note that the above code syntax may be wrong, I know the control name is longer then just ToolStrip, I just used as demo to show you the concept.请注意,上面的代码语法可能是错误的,我知道控件名称比 ToolStrip 长,我只是用作演示来向您展示概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM