开发者

WinForms ToolStrip not available in visual inheritence

Lets say I have FormChild inheriting Fo开发者_StackOverflow中文版rmParent. 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.

I've checked the Internet, it seems this is a well-known bug; but does anyone know a workaround or something?


Well you can manually edit FormChild.Designer.cs file. There you can access inherited toolstrip.

Edit:

  1. Add this 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.
  4. Change Modifier property to protected.
  5. Add items to inherited toolstrip in FormChild.

and you should be able to design control in designer. Tried a bit - seems like it works.

P.s. Iam using 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.

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜