Visual Studio 2015 Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Managing the editor windows

One of the advantages of using a graphical IDE is the ability to have multiple windows open, so learning how to customize their size and layout in Visual Studio is important for maximizing their productivity. Given the multitude of windows (editors, debugger output, and so on) Visual Studio has open, it can be helpful to learn how to place them where you want so that you can focus on your work without being slowed down by clutter.

Tab pinning allows you to mark individual windows so that they stay open while you navigate through the editor. Previewing documents is a useful way to navigate across several files without cluttering your tabs with several open documents. This recipe will explore both options.

Getting ready

To follow along, open a solution of your choice. The following walkthrough uses a brand new Single Page Application ASP.NET 4.6.1 Template, but the concepts apply to any project. Ensure that the Solution Explorer window is open.

How to do it…

The following steps will show how the position of open windows can be changed to your liking. Let's get started:

  1. In the Solution Explorer window, locate the AccountViewModels.cs file in the project folder (under Models), and double-click on it. The source file will open in the main window area, as in the previous versions of Visual Studio; however, you will now notice that the document tab features a pin icon next to the tab name, as you can see in the following screenshot. You'll use that pin in just a few steps:
  2. Using the Solution Explorer window, open both the IdentityModels.cs and ManageViewModels.cs files by double-clicking on them. You should now have three documents open with their tabs showing in the tab well (this refers to the row of tabs for each open document in the editor), as shown in the following screenshot:
  3. Click on the ManageViewModels.cs tab to select it, and then click on the pin. The pin will change to point downwards, indicating that the document is now pinned. Visual Studio will always keep pinned tabs visible in the tab well. These pinned tabs will remain visible, even when Visual Studio has to start hiding unpinned tabs to save screen display space. The pinned document tab will be moved to the left next to any other pinned documents you may have open:
  4. Right-click on the AccountViewModels.cs document tab, and click on the Close All But This option to close all open documents except for the one currently selected. This will include closing any pinned documents, which are shown in the following screenshot:

    Note

    There is a related option Close All But Pinned, which is useful when you would like to only keep pinned files open.

  5. Reopen both files, ManageViewModels.cs and IdentityModels.cs by double-clicking on them in Solution Explorer.
  6. Notice that in VS2015, like in VS2013, double-clicking on a document tab of your choice gives that tab focus, the same as single-clicking on it. Previously, double-clicking on a document tab would cause it to float.
  7. Now lets see how the preview window works in conjunction with the tabs we already have open. Press Ctrl + Shift + F to open the Find in Files dialog box. Enter some search text (we will use login) in the Find what field, and ensure that Look in is set to solution; then click on the Find All button.
  8. In the Find Results 1 window, select a result from a file that is not already open; we will use AccountController.cs.
  9. The file will open in the preview tab located on the right-hand side of the tab well.
  10. The preview tab shows the contents of the currently selected document if it is not already open. In the Find Results 1 window, select a different file. This new file will now appear in the preview tab, rather than clutter your tab well with open files as you search for the exact file that you want.
  11. Assuming you now have found what you are looking for, and want to keep the current file in the preview tab open, either click on the Keep Open icon on the preview tab, or start making changes to the contents of the file. Any document that is changed in the preview tab is automatically promoted to a normal tab. The following screenshot illustrates using the Keep Open icon to promote a document in the preview tab:

Visual Studio will move the document from the preview tab area into the main tab area. The color of the tab will also be changed from blue to yellow (exact colors depend on the theme that you have selected) indicating that the tab is now a normal document tab.

How it works...

Pinning documents works much like pinning does in any other part of Visual Studio, and is very handy for keeping the documents that you are working on regularly within easy reach, especially when you have many documents open at once.

The preview document tab is a great way to prevent tab clutter, and is very useful while debugging deeply-nested code. You may recall that Go To Definition is one function that uses the preview document tab. For example, multiple source files may be opened as you trace a program's operation across methods and classes. The preview document tab helps you cycle quickly through these files, while preventing the tab well from filling up with documents that aren't needed for more than a few moments.

There's more...

As you may expect, there are more ways to use and customize the behavior of the document tabs in Visual Studio.

Single-click preview in Solution Explorer

The preview tab isn't restricted to just the Find Results window. It can also be used from within Solution Explorer. If you activate the Preview Selected Items button in the Solution Explorer toolbar, then every item you click on will be opened in the preview tab automatically. The Preview Selected Items button is a toggle button. If you want to disable the behavior, then you only need to click on the button to deselect it, and the preview behavior will be turned off:

Customizing tab and window behavior

Navigating to Tools | Options | Tabs and Windows in Visual Studio will show the following dialog box:

There are a number of options here that let you control how the tabs behave. Choices like Show pinned tabs in a separate row or Insert new tabs to the right of existing tabs may be helpful to you. Feel free to experiment with the Tabs and Windows settings to get Visual Studio working the way you like it most.