Home | Resume | Blog Brian Ensink's Blog | The DesignerCategory attribute

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

Comments

4/3/2009 1:24:53 AM #

Scott

Very well-written.  I keep telling myself that I need to learn C#, but I have yet to really get into it.

Scott |

4/6/2009 9:54:31 PM #

Eric Petroelje

Hey Brian, saw you commenting on StackOverflow - just thought I'd drop by your blog and say hi

Eric Petroelje |

4/30/2009 12:41:18 PM #

Brenda

It's really very simple, I already tried that. Your explanation is very detailed, so everything was very simple.

Brenda |

5/14/2009 11:31:16 AM #

Medicines

Your blog is excellent, I would very much like to have more such blogs on the Internet.

Medicines |

6/7/2009 11:19:15 PM #

eve

A major annoyance! Thank heaven for the splendid solution posted almost two years ago by thoward37:
vanguard-against-confusion.blogspot.com/.../disable-design-time-support-in-visual.html

eve |

6/8/2009 12:03:36 PM #

eve

An even better (and more logic) solution was incidentally presented in 2004 by Horia Tudosie in a article about the Numeric Edit Box (www.codeproject.com/KB/edit/NumEditBox.aspx) :

using System.ComponentModel;
using System.Windows.Forms;

[DesignerCategory("Code")]
public class NumEditBox : TextBox
{
// (..)
}

eve |

Comments are closed

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.