Friday, August 22, 2008

Adding an inherited custom control into Visual Studio Toolbox

Let's say we created the base control XPanel:
   public class XPanel : Panel
   
and its descendant XFinalPanel:
   public class XFinalPanel : XPanel
   
We are to show the XFinalPanel control in the Toolbox and to hide the XPanel. By default both controls should be visible in the Toolbox. There is a ToolboxItem attribute that allows to manage the control visibility. As it is inherited, it sounds logical to write the following code to solve the task:
   [ToolboxItem(false)]
   public class XPanel : Panel
   ...
   [ToolboxItem(true)]
   public class XFinalPanel : XPanel
Unfortunately, it doesn't work. The XFinalPanel is shown in the Toolbox, but you can't drag&drop it to the form. The only workaround I've found is to declare XPanel as abstract class. Or, of course, you may let both controls be shown in the toolbox.