4

Lets say I have FormChild inheriting 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.

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

Denis Biondic
  • 7,943
  • 5
  • 48
  • 79
  • Where did you see this listed as a "well-known bug"? Are you talking about [this bug report](https://connect.microsoft.com/VisualStudio/feedback/details/114535/visual-inheritance-is-not-supported-by-some-new-controls)? The solution is to make the modifications programmatically, rather than via the Designer. Seems simple enough; add your code in the derived form's constructor. – Cody Gray - on strike Jul 14 '11 at 07:29
  • Yes, that is the exact bug, but I just wondered If anyone has a solution for Visual Studio designer? I know I can access the toolstip programatically – Denis Biondic Jul 14 '11 at 07:36
  • 1
    I don't think so. As the Connect page indicates, you'll either need to write your own custom designer for the `ToolStrip` (hardly recommended; that's treading some pretty undocumented waters), or just make the changes programmatically. I'm not sure what the disadvantage is of doing this with code, though. The Designer is meant as an aid for the developer, but I see too many people hesitant to ditch it when it starts getting in their way. – Cody Gray - on strike Jul 14 '11 at 07:37
  • What version of Visual studio are you using. – Jethro Jul 14 '11 at 07:44

3 Answers3

4

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

Renatas M.
  • 11,694
  • 1
  • 43
  • 62
  • The problem is not to manipulate the toolstrip programmatically – Denis Biondic Jul 14 '11 at 07:35
  • 3
    Yes, the designer will overwrite those changes. You should *never* edit a `Designer.cs` file by hand. – Cody Gray - on strike Jul 14 '11 at 07:36
  • @CodyGray Not recommended, but I do edit the designer.cs file, usually inside the #region block. Changes are reflected in the design. If you are careful, it can improve productivity over using the designer. Changes made in the constructor following the InitializeComponent() often don't work out well, and you don't see the changes in the designer. It *can* break the code (the feared *To prevent possible data loss ...* screen). If I break the form (by editing the designer, modifying the base class, whatever), I have been able to fix it by editing the designer.cs file. Comments? – riderBill Jan 22 '15 at 18:29
0

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

  • 1
    Welcome to Stack Overflow! Thanks for posting your answer! Please be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully, just FYI. – Andrew Barber May 21 '12 at 02:17
-1

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.

Jethro
  • 5,896
  • 3
  • 23
  • 24
  • Could you please expain how should this solve the problem exactly? My toolstrip is declared in parent, manipulated with designer. When child form inherits the parent, it gets this toolstip control also; but the problem is in VS desginer which doesn't allow changing of toolstrip in form inheritence scenarios (for all other controls as I know it works fine) – Denis Biondic Jul 14 '11 at 07:32
  • How does your child form inherit the parent. Can you paste code so I can see please. – Jethro Jul 14 '11 at 07:38