Visual Studio Hacks [Electronic resources]

Andrew Lockhart

نسخه متنی -صفحه : 172/ 34
نمايش فراداده

Hack 11. Select the Best Editor

Different editors can be used for different file types, including some hidden possibilities.

A large part of learning to work with any toolset is understanding what tool should be used and when it should be used. Visual Studio contains a number of different editors, including a Text Editor, XML Editor, CSS Editor, and many more. This hack is about determining what editor should be used in what situation and also explains how Visual Studio can be configured to use a third-party editor as opposed to the native editor.

2.7.1. So Many Editors

Visual Studio contains a host of different editors; you have probably encountered a number of them.

2.7.1.1 Text Editor

The editor you are probably most familiar with is the Text Editor, which is used to edit and work with most forms of text, including source code. The Text Editor contains a number of configuration settings that can be specified on a per-language basis; these settings can be accessed through the Tools Options menu, which can be seen in Figure 2-22.

Figure 2-22. Text Editor Options dialog

Using the Options dialog, you can decide whether or not to enable word wrap, line numbers, and some other miscellaneous settings. Each language can be configured independently of the other languages; each is configured to different defaults initially.

As of Visual Studio .NET 2003, the VB.NET editor is set to Hide Advanced Members automatically. This is something you will most likely want to turn off, as it stops certain methods and properties from showing up in IntelliSense. Some of the hidden methods, like Page.RegisterClientScriptBlock, are very valuable and encourage better development and so should not remain hidden. The idea behind this setting is to filter members that won't normally be used by your average developer, but as it turns out, some of the members it hides are indeed very useful. Methods marked with the attribute [EditorBrowsable(EditorBrowsableState.Advanced)] will be hidden when this option is enabled.

The Text Editor is used for programming languages, so it's the one you will probably work with the most.

2.7.1.2 HTML/XML Editor

The HTML/XML Editor is used to work with HTML, XML, and XML schemas. The HTML/XML Editor consists of a regular textual view of the HTML or XML as well as a number of different designers. Designers provide a visual representation of the HTML or XML that you are editing. A designer for HTML shows you how the HTML should look in the browser and provides WYSIWYG editing support. A designer for XML shows a "Data" view by creating rows and columns based on the structure of the loaded XML. A designer for XML schemas shows a visual representation of the schema and allows you to drag elements from a schema-specific toolbox.

For the most part, these editors are all self-explanatory and work perfectly, but if you are particular about your HTML then you probably don't like Visual Studio messing with it. Visual Studio has a nasty habit of not only reformatting your HTML, but sometimes even removing attributes or tags.

Visual Studio is getting better and better at working with HTML. Visual Studio 2005 is light-years ahead of previous versions when it comes to leaving your HTML alone and generates even better HTML than previous editions.

HTML purists will always want complete control over their HTML, how it is formatted, and what is included. The best way to manage this in Visual Studio is to simply not use the designer, but since by default, all l and .xml documents are opened with the designer, you will need to change some settings to get this behavior.

To switch the default behavior, you need to change the settings located under the HTML Designer folder in the Tools Options dialog. These settings are shown in Figure 2-23.

Figure 2-23. HTML Designer Options

On this Options screen, you can determine what view will be used for each type of file handled by the HTML designer. Changing the radio button to HTML View (in which you edit the raw HTML) for each of the file types will cause Visual Studio to also use the HTML view when opening these files. (You can later switch to design view if you like.)

2.7.1.3 Other editors

There are also editors for CSS files, resource files, and binary files. I am not going to cover these here since they are easy to use and don't have any hidden features that you need to worry about.

2.7.2. Choosing Your Editor

Visual Studio will also select what editor it feels is best, based on the extension of the file you select, but sometimes it is beneficial to use a different editor than the Visual Studio default. To select a different editor, you need to use the Open With command, which you can access in a couple of different ways. When selecting a file from the Solution Explorer, right-click the file and choose Open With from the context menu that appears. Or if browsing for a file with the File Open dialog, first select the file, then click the arrow on the Open button (see Figure 2-24), and choose Open With.

Figure 2-24. Open With option

After selecting Open With, you will see the Open With dialog, which is shown in Figure 2-25.

Figure 2-25. Open With dialog

You can see in Figure 2-25 that the Web Service Editor is the default editor for the .asmx extension. This is a great example of when you need to specify a different editor: by default you can view only a designer of the web service or the web service's code behind (the file that contains any C# or VB.NET for your web service) .asmx file. There is no way to actually view and edit the source code of the .asmx fileyou must go through the designer. To overcome this restriction, select the HTML/XML Editor from this dialog, and you will be able to directly edit the .asmx file just as if it were a normal ASP.NET file.

This dialog also allows you to set the default editor for a file extension; this is particularly useful when working with a new extension that Visual Studio might not be set up to edit. Loads of different applications use different extensions for what boil down to be XML files. When you first open one of these files, just select the HTML/XML Editor and click the Set As Default button (shown in Figure 2-25). Whenever you open this file type in the future, Visual Studio will use that editor.

2.7.3. Use a Third-Party Editor

Although Visual Studio includes a number of nice editors, sometimes a third-party application does it better. I have a particular liking for a CSS editor application called TopStyle (http://www.bradsoft.com), and whenever I work with CSS files, I use it.

To configure TopStyle as an editor in Visual Studio, all I need to do is open a .css file, use the Open With option, and from the Open With dialog, click the Add button. I then see the Add Program dialog, which is shown in Figure 2-26.

Figure 2-26. Add Program dialog

From the Add Program dialog, I can browse for the program executable and also give the application a friendly name, which will be shown in the editor list. After adding TopStyle as an editor, I can then click OK to return to the Open With dialog and there set TopStyle as the default editor for .css files by selecting it in the editor list and clicking the Set As Default button. Now whenever I double-click on a .css file, Visual Studio will launch an instance of TopStyle with the file open in it. If the file is under source control, you will need to manually check it out before opening it with the third-party editor.