Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Visual Inheritance?

I'm working on a WinForms app that has an ideal scenario for visual inheritance, but after 8 hours of struggling to get it to work I'm wondering if I'm wasting my time.  Changes I make to the base control seem to intermittently not propagate to the derived controls.  Looks like the Forms Designer code in the base control gets very "confused" at times.  This confusion is worse if there is a file reference between projects.

Has anybody successfully coded visual inheritance with UserControls?

Joe Paradise
Tuesday, January 11, 2005

Could you be more specific?  Are you using a UserControl, or are you trying to inherit from a standard control like, say, Button or TreeView?

Generally the easiest way to create an inherited control is to create a new class using Add Class.  Name the class whatever you want to call your version of the control, and then put in an inherits line, e.g.:

Public Class MyTreeView
    Inherits System.Windows.Forms.TreeView

End Class

Then save that file, and your class will turn into a custom control.  You won't see what the control looks like in design view; you'll have to put it on a form to actually see it.  To add it to the toolbox is not very intuitive; you have to:

Compile your project (Ctrl-Shift-B).
Right-click on the Toolbox.
Choose "Customize Toolbox..."
Click the ".NET Framework Components" tab.
Click "Browse..."
Find the "bin" folder of your project.
Add the .exe or .dll of the project containing your custom control.

This should add it to the toolbox and then you can drag it to a form to see it in action.

Tuesday, January 11, 2005

Yeah, I should have been more specific.  Chalk it up to battle fatigue.

I have a control inheritance hierarchy four layers deep spread over two control libraries (file reference between projects).  The root control inherits from UserControl.  Sounds complicated, but solves the UI problem quite well.  The specific problem I’m having is that changes to the base controls in the Visual Studio Designer do not propagate to the derived controls’ Designer if they are in the second project. 

After testing I’ve found the cause is my old nemesis:  file references.  When the two projects have “project references”, the derived control designers will update when they receive the focus (click anywhere inside the designer).  If the two projects are related with a “file reference”, the derived designers will only update if you build the solution and restart Visual Studio.

Microsoft’s implementation of file references between projects is buggy as hell.  Multiple file references can give erroneous build errors when the compiler incorrectly sees a referenced project output as locked and fails to copy the output file for dependent projects (Google “Cannot copy assembly”).  Now they can add visual inheritance to the list of features affected.

Joe Paradise
Tuesday, January 11, 2005

Right, well, uh, that would have been my next suggestion.  Yep.

Well, maybe what I wrote will help someone else, anyway.  VS is very confusing on this because the logical choice to create an inherited control seems to be "Inherited Control" but that's usually not what you really want.

Tuesday, January 11, 2005

There are several links on this I can send you.

Create new web projects in your solution.



Now, create new web project, call it


remove global and web.config.

Add a reference to the project in MyMainWebApp - (not browse to dll)

Goto IIS, goto MyControls - remove as application (will goto a folder).

Build solution.

This is what we do to share different controls across projects in one solution.

Works quite well.

Also - we have a base class web library project that all our pages and controls are derived from that contains all the low level processing like security.

I never browse to a dll to add a reference unless it's third party - I add project reference.

Also - build order can be a factor, which you have control over.

Hope this helps.

Thursday, January 13, 2005

I typed all that and see you said winforms.

Sorry - my mistake, I was thinking web.

Thursday, January 13, 2005

Thanks for posting anyway.  It was informative to hear that you're using project references vice file references.  I think most people don't run into this because they use project references exclusively.

Hope MS fixes this in Whitbey; been fighting this since VS 2002.

Joe Paradise
Thursday, January 13, 2005

*  Recent Topics

*  Fog Creek Home