• 0

Visual Inheritance of a ToolStrip


Question

Hi,

This is a known and previously reported problem and promised to be fixed in the "NEXT" version of Visual Studio over and over again. What we need is all developers racking up to the issue I created at Microsoft Connect and start pushing the number of approvals high enough so the VS team can get off their ass and fix this things once and for all.

Come on guys, go to the issue and vote for it to be fixed and confirm that you can reproduce this bug.

Microsoft Connect : http://connect.microsoft.com/VisualStudio/feedback/details/552017/visual-inheritance-of-a-toolstrip

I am trying to create an infrastructure library to be used by all my projects where one of the project in that library contains the base forms for different things like ViewBase, EditBase, SearchBase & DialogBase. They all are inherited from a Base form which contains a toolstrip control with 3 common buttons for OK, Close & About.

Now depending on the type of base form I would like to add few more buttons and controls on the toolstrip during the design time. For example the SearchBase would have a Search, Clear buttons and so forth and so on.

It it worth noting that I change the modifier of the toolstrip to Protected. None of the inherited form allows me to add a new button or anything on the toolstrip. So I made the modifier public and the same behaviour. All the properties of the toolstrip control are disabled. Why is this? This is kind of a show stopper as I can't rely on the standard controls provided and have to look for third party controls.

Steps to reproduce this.

Create a new blank solution

Add a new project of type class library

Delete the default Class1 file

Add a Windows Form named "FormBase"

Add a ToolStrip control on the form and set the modifier to "Protected"

Add a button and a label (Don't have to but this is for visual testing)

Compile the solution

Now add an Inherited Windows Form named "FormSearchBase" and inherit it from the "FormBase"

I have posted this to Microsoft Connect as a bug but if it gets more support and approval then there is a higher chance of it being fixed.

Cheers :)

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

What I think you are missing is that the way the WinForms designer works; when it generates code, it's actually just generating code which isn't executed till runtime. The designer simply provides a visual representation of that resolved code. When you inherit a form, the InitialiseComponent() method (which is private, and not virtual) is executed through the constructor of the base class. You then open the form which inherits from your base form, but the controls placed on the base form can't be designed, because the designer is now working with a different designer file.

i.e.

Form1.cs <- constructor, any other code.

Form1.Designer.cs <- design time code.

Form2.cs <- constructor, any other code, inherits from Form1

Form2.Designer.cs <- design time code.

You can't make the designer generated method InitialiseComponent() protected and virtual because the designer invokes this method through Reflection, and is expecting a private method, so it won't be able to find it, not to mention the fact that it is regenerated when you make changes in the designer.

If you want to enable the scenario you are suggesting, the designer is probably not the way to do it. It's better do this through code, adding things manually, but controlled. You just lose design time support.

Edited by Antaris
WinForms, not WebForms!
Link to comment
Share on other sites

  • 0

Wow a very decent response which is what I was hoping for.

It's not a webforms designer but it's a windows forms application. I know what you mean by the default constructor and all but the visual inheritance works for all the other controls except, ToolStrip, MenuStrip and few base controls that a developer can't live without in a large scale applications that needs a massive amount of visual inheritance. All third party components do support this and I would have expected this behaviour from Microsoft's flagship product and common controls that we all are so much used to. The main reason to raise this issue is, this was partially working in Vs2003, people complained so they promised to fix this in VS2005 as it was too late to fix in VS2003. Along came VS2005 it was not working at all and the same promise to fix this in VS2008. Now we are at VS2010 and same story!

Link to comment
Share on other sites

  • 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 - Visual Inheritance For MenuStrip, ToolStrip and StatusStrip Controls

  • Like 1
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.