IanG on Tap

Ian Griffiths in Weblog Form (RSS 2.0)

Blog Navigation

April (2018)

(1 item)

August (2014)

(1 item)

July (2014)

(5 items)

April (2014)

(1 item)

March (2014)

(1 item)

January (2014)

(2 items)

November (2013)

(2 items)

July (2013)

(4 items)

April (2013)

(1 item)

February (2013)

(6 items)

September (2011)

(2 items)

November (2010)

(4 items)

September (2010)

(1 item)

August (2010)

(4 items)

July (2010)

(2 items)

September (2009)

(1 item)

June (2009)

(1 item)

April (2009)

(1 item)

November (2008)

(1 item)

October (2008)

(1 item)

September (2008)

(1 item)

July (2008)

(1 item)

June (2008)

(1 item)

May (2008)

(2 items)

April (2008)

(2 items)

March (2008)

(5 items)

January (2008)

(3 items)

December (2007)

(1 item)

November (2007)

(1 item)

October (2007)

(1 item)

September (2007)

(3 items)

August (2007)

(1 item)

July (2007)

(1 item)

June (2007)

(2 items)

May (2007)

(8 items)

April (2007)

(2 items)

March (2007)

(7 items)

February (2007)

(2 items)

January (2007)

(2 items)

November (2006)

(1 item)

October (2006)

(2 items)

September (2006)

(1 item)

June (2006)

(2 items)

May (2006)

(4 items)

April (2006)

(1 item)

March (2006)

(5 items)

January (2006)

(1 item)

December (2005)

(3 items)

November (2005)

(2 items)

October (2005)

(2 items)

September (2005)

(8 items)

August (2005)

(7 items)

June (2005)

(3 items)

May (2005)

(7 items)

April (2005)

(6 items)

March (2005)

(1 item)

February (2005)

(2 items)

January (2005)

(5 items)

December (2004)

(5 items)

November (2004)

(7 items)

October (2004)

(3 items)

September (2004)

(7 items)

August (2004)

(16 items)

July (2004)

(10 items)

June (2004)

(27 items)

May (2004)

(15 items)

April (2004)

(15 items)

March (2004)

(13 items)

February (2004)

(16 items)

January (2004)

(15 items)

Blog Home

RSS 2.0

Writing

Programming C# 5.0

Programming WPF

Other Sites

Interact Software

Opening Component Classes in Code View in Visual Studio .NET

Thursday 10 June, 2004, 11:41 PM

By default, when you open a source file in Visual Studio .NET it shows a design view if it can, whether it's useful or not. Often it is useful - for example, the design view for a class derived from System.Windows.Forms.Form allows WYSIWYG editing of the form layout. But it sometimes opens a design view that offers no benefit.

For example, there is a design view for any class derived from System.ComponentModel.Component. To be fair, this view has its uses: if your class has an InitializeComponent method, the design view lets you populate that by dragging non-visual items from the Toolbox onto the design surface. You can then select those items and edit them in the Property grid.

That's all perfectly reasonable. What I object to is when I've written a class which derives from System.ComponentModel.Component, but which doesn't have an InitializeComponent method. It's true that the design view will happily add one for me if one isn't present. (Although it won't add code to the constructor to call it.) But I usually find that if I have a Component-derived class that doesn't have an InitializeComponent method, it's because I don't want to use the design mode to edit it - it just contains some code. (Custom controls are the example I keep running into, but I've hit this with non-visual components too.)

Of course you can just change the VS.NET default for how it opens files. Simply right-click on the file in the editor and choose 'Open With...' from the menu. Select CSharp Editor (or VB Editor, or whatever you preferred language is), making sure you choose the one without 'Form Editor' in the name. Then click the 'Set As Default' button. VS.NET will then default to using the code editor from now on.

The problem is, it will do it for everything. What if you want it to carry on opening the design view for those files where this is useful, and only open a code view for specific files?

Well you can place an indication in a file that will cause VS.NET to always default to a particular editor for that file, regardless of what the current VS.NET default may be. Simply apply the System.ComponentModel.DesignerCategory attribute to the class. For example this class:

[System.ComponentModel.DesignerCategory("Code")]
public class MyComponent : System.ComponentModel.Component
{
   ...
}

will always open in the Code view by default, even if VS.NET is configured to open the design view by default. (Of course, the user can always explicitly choose which view to open by right-clicking on the item in the Solution Explorer. This simply affects what happens when the user double-clicks.)

Note that you must use the attribute's namespace-qualified name, even if you have a using statement importing the System.ComponentModel namespace - VS.NET only honours the DesignerCategoryAttribute if you write out the fully qualified class name.

Copyright © 2002-2024, Interact Software Ltd. Content by Ian Griffiths. Please direct all Web site inquiries to webmaster@interact-sw.co.uk