The DesignerCategory attribute

by Brian Ensink 2. April 2009 23:44

Today I found a simple workaround for a minor annoyance in Visual Studio 2008.  I expect the annoyance and workaround to apply to Visual Studio 2005 as well but have not verified it.

The annoyance is with Visual Studio's design mode support.  I have a class that inherits from a class in a third party library.  My class is in its own .cs file in the project.  Whenever I double click on the file Visual Studio opens the class in Design Mode however there is no design mode tool for this class and I simply get a blank design screen with a link to view the code.  I can follow the link or right click on file and select "View Code".  but instead I want the code editor to open when I double click this file.

Using .NET Reflector I discovered that the third party class I am inheriting from is a subclass of System.ComponentModel.Component.  The Component class has the following attribute declaration which you can see using .NET Reflector to disassemble the class declaration.

image

The DesignerCategory attribute is associating my subclass with the component designer.  To change this default behavior of the subclass just add the attribute to the subclass and specify the blank string for the design tool.

   1: [System.ComponentModel.DesignerCategory("")]
   2: internal class MyClass
   3: {
   4:     ...

You can easily try this in a project by adding a new blank C# file and adding the following class to the file.  (Don't use Visual Studio to automatically add a new form although you can actually apply the attribute there as well but in this case you won't be able to get back to the Forms designer without removing the attribute.)

   1: class Form1 : Form
   2: {
   3:     public Form1()
   4:     {
   5:     }
   6: }

If you double click on the file it will actually open in the forms design tool, but suppose you don't want to use the design tool.  Simply add the DeisgnerCategory attribute shown below.  Now when you double click on the file it will open directly in the code editor.

   1: [System.ComponentModel.DesignerCategory("")]
   2: class Form1 : Form
   3: {
   4:     ... 

Three minor points of interest I found while trying this workaround.

  1. You must pass an empty string ("") to the attribute's constructor.  The documentation seems to suggest the zero-argument constructor does the same as passing an empty string but it does not appear to actually override the value set by the Component class far up the inheritance tree.
  2. If you have ever opened up the project's .csproj file you will have seen the <SubType> element to tell Visual Studio to open the Forms editor (or some other design tool).  Just deleting that element isn't enough as it will be reinserted when Visual Studio reloads the project.
  3. Be sure to checkout the .csproj file from your source control provider before adding this attribute.  The attribute and the <SubType> element go hand-in-hand but just adding the attribute isn't enough to trigger Visual Studio to automatically checkout the project file.

Tags:

Software Development

About the author

I am currently a .NET developer and really enjoy the platform.  .NET seems to be able to take the developer whereever he/she wants to go.  To the desktop, to the web, to a database, etc.  At my day job I write desktop apps but I also like to toy with other tech as I have time.