VS 2008 and inherited forms


Inherited forms are a great idea and have been around in .NET since the 1.0 beta.  However when used in VS 2008 there’s an annoying problem.  This Microsoft KB article touches on it but doesn’t cover all the problems or their solutions.  Moreover in some scenarios the suggested fixes do not work because the problem is different.

The additional issue affecting me is this: I create a base form and put a panel on it because the panel can display a nice gradient background.  I set docking to Fill so it covers the base form’s client area.  OK and Cancel buttons are added that I anchor to the bottom of the form.  That’s it, a very basic form.  For good measure I follow the knowledgebase article and make the base form control protected rather than private.  Now I compile the application so the base form appears as an inheritable form and create a new form that inherits from the base form. 

Because the new form size is too small I expand it.  The button moves as expected and everything looks great.  I save it, close it and open it again but the buttons have gone.  Well, they’ve not gone they’re hiding off the visible area.  So what’s going on and how to fix it?

The problem seems to be that in their wisdom the designers of System.Windows.Forms forgot that an inherited form may contain a docked control.  So when the inherited form is designed the inherited form expands and docks the docked control (correct) and then the base form expands and docks the docked control (incorrect).  The result is that the controls are repositioned outside the visible area.  Great.

To fix the problem, the first thing to do is to undock the contained panel.  Then size the inherited form and set the previously docked panel to cover the inherited forms client area.  Finally use anchors to ensure the panel size tracks the size of the inherited form.  It’s that easy. 

Of course if you control the base form you can change it so the panel uses anchors rather than the dock property in the first place.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

[…] Dev Blog wrote an interesting post today on VS 2008 and inherited formsHere’s a quick excerpt This Microsoft KB article touches on it but doesn’t cover all the problems or their solutions…. […]