File Server Migrator


Customers of Sphere 10 Software

Visual Inheritance For ToolStrip Controls

By Herman Schoenfeld, May 20 2012

Download Source Code

Visual Inheritance in Windows Forms

Visual Inheritance is a great little feature provided by Visual Studio that allows Forms (in WinForms) to inherit their presentation and behaviour from base forms in much the same way classes inherit from base classes.

Developers can streamlime development of a WinForms presentation-tier by isolating as much presentation and functionality as possible in the base forms and then re-using that presentation in the sub-forms via the Designer.

Just as code inheritance, Visual Inheritance enhances productivity, reduces the amount designer code-writing, increases the amount of designer-code re-use which reduces the amount of bugs thus providing better overall quality.

At least, that's the idea.

This video provides a basic overview of Visual Inheritance.

Limitations of Visual Inheritance

In typical Microsoft style, this functionality is broken.

Whilst it works for almost all the controls, Microsoft have intentionally disabled it for complex controls such as the MenuStrip and ToolStrip. As a result, Visual Inheritance is essentially unusable as developer might as well code the whole form every time if he has to code half of it anyway (as comment after comment on the web reveals).

Microsoft say they have disabled Visual Inheritance for the ToolStrip controls because their developers can't handle the complexity involved in designer-serializing child item controls across an inheritance tree, but there are ways to do this easily. Microsoft promise time and time again to deliver this feature, but have fail to do so every time. As a result, with each new version of Visual Studio, loads of new bug reports are lodged and many new developers are left disappointed (and maybe look favorably at iOS development).

Overcoming the limitation

The simple solution (apparantely too simple for Microsoft) is to create an invisible toolstrip on the sub-form and merge it with the toolstrip in the base-form.

On the sub-form toolstrip we set the MergeAction and MergeIndex of the menu's and their items to what they need to be to get the effect we want on the base-form toolstrip when merged.

We then merge the sub toolstrip with the base toolstrip at load time!

The sub-form toolstrip is made invisible because its items are moved into the base menu strip when the form is loaded. We only care about the toolstrip at design time as it allows us to define the items we want merged into the base-form toolstrip.

The way we access it at design time is by selecting the toolstrip in the component panel down the bottom. The designer temporarily makes it visible for us.

That's basically it!

Download the components and try it for yourself. They have some added design-time support to help you see what you're merging.

MenuStrip with Visual Inheritance

This video demonstrates how we can achieve Visual Inhertiance of a base-forms MenuStrip.

ToolBarStrip with Visual Inheritance

This video demonstrates how we can achieve Visual Inhertiance of a base-forms ToolBarStrip.

StatusStrip with Visual Inheritance

This video demonstrates how we can achieve Visual Inhertiance of a base-forms StatusStrip.

Company Awards