Visual Studio 2005 Tips and Tricks
Refactoring
VS 2005 Includes a set of refactor and “surround with” operations, a subset of Resharper’s functionality if you're familiar with that tool.
- Right click->Refactor or the Refactor menu will show you the available refactorings. You can also perform many refactorings just by making text changes. For example, if you edit the name of a method in the IDE, VS will display a smart tag (a small rectangle near the bottom right of the current token – hover over it to expand) allowing you to rename the method everywhere it’s used, either with or without a preview of the changes first.
- Smart tags may also be expanded (when the small rectangle is visible) using the keyboard by pressing Shift+Alt+F10 or Ctrl-. (That last one is the Control button plus the period button.)
- Similarly, if you type the name of a class that is defined in the project’s current References, but is not imported via a using statement, VS will provide you with a smart tag allowing you to either fully qualify the name (e.g. System.Text.StringBuilder) or add a using statement.
- Right click->Surround With is helpful for adding namespaces, exception handling, etc.
- All of these refactorings have keyboard shortcuts:
- Encapsulate Field: Ctrl-R, E
- Extract Interface: Ctrl-R, I
- Extract Method: Ctrl-R, M
- Promote Local Variable: Ctrl-R, P
- Remove Parameters: Ctrl-R, V
- Rename: Ctrl-R, R
- Reorder Parameters: Ctrl-R, O
- Ctrl-K, S brings up the Surround With dialog.
- You may also note that Intellisense has been improved; in most cases, as soon as you start typing the name of a class, method, etc, the Intellisense window will automatically pop up.
Code Snippets
VS also includes code snippets, which are extensible bits of code that are inserted to speed up common operations.
- You can bring up the list of snippets by typing Ctrl-K, Ctrl-X, or insert a specific snippet by typing the short name of the snippet and pressing Tab twice.
- Snippets have “fields” that are designed to easily fill out the customizable portions of the inserted code.
- For instance, in a class, typing “prop” (without the quotes) and then Tab twice will expand out to a full property declaration with both a getter and a setter as well as a backing private variable. The type of the property and the name of both the private var and the property itself will be green; you can use the Tab key to switch between these, retyping them as necessary, and VS will keep your changes in sync so that changing the type of the private var changes the type of the property, etc. Hitting Escape will mark the snippet complete (and remove the green/tabbable behavior), as will typing outside the snippet.
- As I noted, snippets are extensible; a number of them are already available from MSDN.
DataTips/Debug Visualizers
DataTips are enhanced tooltips that appear when you hover over a variable or value while debugging, which allow you to edit and visualize values very easily. DataTips are described here.
Visualizers are plugins to Visual Studio that allow you to view variables in more useful ways than the Watch window or even DataTips. Some of the default visualizers do things like allow you to open a string in a larger window for easier reading; view a DataSet or DataTable as a grid of rows and columns; etc. You can get to a visualizer at debug time by clicking the magnifying glass visible in the DataTip or Watch window row.
There are several visualizers already available, including
- ASP.Net Cache Visualizer
- ASP.Net Control Visualizer (included with Cache visualizer)
- Regular Expression Visualizer
- A very nice XML Visualizer
Other Add-Ins
James Avery wrote an article for MSDN Magazine listing “Visual Studio Add-Ins Every Developer Should Download Now” available here
Online Help
VS 2005 also includes the ability to automatically search for help online as well as locally. However, this is pretty slow, so I recommend you go to Tools>Options>Environment>Help>Online and change the first option to “Try local first, then online” to speed things up. Note that searching help also searches a number of online forums (the list is on that same panel).
Macros
Here is a macro to collapse all projects in a solution and expand the selected project:
- Select ALT+F8 on your VS2005 IDE to open the macro explorer
- Right Click MyMacros.
- Select New module.
- Type the new module name as CollapseAll
- Select OK – A new module CollapseAll is created.
- Right Click CollapseAll Module
- Select Edit – The Macro IDE is launched.
- Copy the following Sub and paste it between the module and End module. -- in the Macro IDE (you may need to adjust some lines in order to successfully compile the macro):
Sub CollapseAll()
'NavigateSolution()
' Get the the Solution Explorer tree
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count = 0)
Then
Return
End If' Get the top node (the name of the solution)
Dim UIHSolutionRootNode As UIHierarchyItem
Dim UIHChildItem As UIHierarchyItem
UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)' Collapse each project node
Dim UIHItem As UIHierarchyItem
For Each UIHItem In UIHSolutionRootNode.UIHierarchyItems
For Each UIHChildItem In UIHItem.UIHierarchyItems
UIHChildItem.UIHierarchyItems.Expanded = False
Next
UIHItem.UIHierarchyItems.Expanded = False
Next
UIHSolutionRootNode.UIHierarchyItems.Expanded = TrueDim UIHSelectedItem As UIHierarchyItem = UIHSolutionExplorer.SelectedItems(0)
UIHSelectedItem.UIHierarchyItems.Expanded = True
End Sub
Save the module.
Now add this macro to the toolbar for easy access. - Select Tools -> Customize from the main menu
- Select the Command tab in the Customize dialog
- Select Macros
- Select the CollapseAll macro and drag and drop it on one of VS2005 IDE toolbars. You will see the Macro Name on the ToolBar –
- Do not Close the Customize Popup window --
- Right Click the Macro Name on the ToolBar
- Select Default Style – This will remove the Macro Name on the ToolBar, resulting in a small Rectangle on the ToolBar
- Right Click the Small Rectangle
- Select ChangeButtonImage
- Select one of the images, for example, the HourGlass. You will see the Image on the toolbar.
- Close the Customize PopUp
- To test the macro:
- Select one of the projects in your solution
- Select the CollapseAll Button on the ToolBar
- Observe the Solution Explorer will collapse all and will expand the project you have selected.
IDE Options
Some useful settings in Tools > Options:
- To get line numbers when working in C# code files: Select TextEditor / c# / General -- check line numbers
- If you want the file you are working on to be highlighted in the solution explorer: Select Projects and Solutions / General -- Check Track Active item in the solution explorer
- If you want to know what’s going on when you are building a solution: Select Projects and Solutions / General --- Check Select Show output window when build starts
- To prevent the compiler from performing HTML validation on web projects (which can be slow): Select HTML / Validation -- Uncheck the validation check box.
Miscellaneous
Note that the “add using” smart tag will not appear if the current project does not have a reference (Project > Add Reference) to the required library.