About Author

Shabir has 12 yrs of exp in I.T using Asp, Asp.NET, Vb6/.NET/C#, MOSS/WMI/html/DOM, Ajax, XML,xsl.SharePoint

Name:Shabir Hakim
Country: India
Gender: Male
Visual Studio shortcut keys
You are familiar with many of Visual Studio's shortcut keys, but not all of them. Here is a handy reference that can make your .NET lifestyle easier and a lot more productive.

Note:the 'must-know' shortcut keys are highlight

Summary:Visual Studio 2005 is the leading developer tool on the market, and I would like to share with you some tips and tricks that will make this great tool even more powerful. Getting familiar with a tool is crucial to getting the most out of the tool, and development tools and IDEs are no different. But with the many new technologies such as C# 2.0, ASP .NET 2.0, Windows Workflow Foundation, Windows Presentation Foundation, and Windows Communication Foundation, who has time to learn about Visual Studio itself? By spending 10 minutes to read this article, I hope you will learn a couple of useful things that will make your life inside Visual Studio more pleasant and productive

Project related
Shortcut Description
Ctrl-Shift-B
Builds the solution
Ctrl-N
Displays the New File dialog. Note: files created this way are not associated with a project. Use Ctrl-Shift-A to add a new file in a project
Ctrl-Shift-N
Displays the New Project dialog
Ctrl-O
Displays the Open File dialog
Ctrl-Shift-O
Displays the Open Project dialog
Shift-Alt-A
Displays the Add Existing Item dialog
Ctrl-Shift-A
Displays the Add New Item dialog
Ctrl-Alt-Insert
Allows you to override base class methods in a derived class when an overridable method is highlighted in the Class View pane
Window manipulation
Shortcut Description
Shift-Alt-Enter
Toggles full screen mode
Ctrl-+
Goes back to the previous location in the navigation history. (For example, if you press Ctrl-Home to go to the start of a document, this shortcut will take the cursor back to wherever it was before you pressed Ctrl-Home.)
Ctrl-Shift-+
Moves forward in the navigation history. This is effectively an undo for the View.NavigateBackward operation
Ctrl-F4
Closes the current MDI child window
Shift-Esc
Closes the current tool window
Ctrl-F2
Moves the cursor to the navigation bar at the top of a code view
Ctrl-Tab
Cycles through the MDI child windows one window at a time
Ctrl-F6,
Ctrl-Shift-Tab
Moves to the previous MDI child window
Alt-F6,
Ctrl-Shift-F6
Moves to the next tool window
Shift-Alt-F6
Moves to the previously selected window
F6
Moves to the next pane of a split pane view of a single document
Shift-F6
Moves to the previous pane of a document in split pane view
Ctrl-Pagedown
Moves to the next tab in the document or window (e.g., you can use this to switch the HTML editor from its design view to its HTML view
Ctrl-PageUp
Moves to the previous tab in the document or window
Control editor (designer)
Shortcut Description
Ctrl-Down Arrow
Moves the selected control down in increments of one on the design surface

Down Arrow
Moves the selected control down to the next grid position on the design surface
Ctrl-Left Arrow
Moves the control to the left in increments of one on the design surface
Left Arrow
Moves the control to the left to the next grid position on the design surface
Ctrl-Right Arrow
Moves the control to the right in increments of one on the design surface
Right Arrow
Moves the control to the right into the next grid position on the design surface
Ctrl-Up Arrow
Moves the control up in increments of one on the design surface
Up Arrow
Moves the control up into the next grid position on the design surface
Tab
Moves to the next control in the tab order
Shift-Tab
Moves to the previous control in the tab order
Ctrl-Shift-Down Arrow
Increases the height of the control in increments of one on the design surface
Shift-Down Arrow
Increases the height of the control to the next grid position on the design surface
Ctrl-Shift-Left Arrow
Reduces the width of the control in increments of one on the design surface
Shift-Left Arrow
Reduces the width of the control to the next grid position on the design surface
Ctrl-Shift-Right Arrow
Increases the width of the control in increments of one on the design surface
Shift-Left Arrow
Increases the width of the control to the next grid position on the design surface
Ctrl-Shift-Up Arrow
Decreases the height of the control in increments of one on the design surface
Shift-Up Arrow
Decreases the height of the control to the next grid position on the design surface
Search and replace
Shortcut Description
Ctrl-F
Displays the Find dialog
Ctrl-Shift-F
Displays the Find in Files dialog
F3
Finds the next occurrence of the previous search text
Ctrl-F3
Finds the next occurrence of the currently selected text or the word under the cursor if there is no selection
Shift-F3
Finds the previous occurrence of the search text
Ctrl-Shift-F3
Finds the previous occurrence of the currently selected text or the word under the cursor
Ctrl-D
Places the cursor in the Find/Command line on the Standard toolbar
Alt-F3, H
Selects or clears the Search Hidden Text option for the Find dialog
Ctrl-I
Starts an incremental search—after pressing Ctrl-I, you can type in text, and for each letter you type, VS.NET will find the first occurrence of the sequence of letters you have typed so far. This is a very convenient facility, as it lets you find text by typing in exactly as many characters as are required to locate the text and no more. If you press Ctrl-I a second time without typing any characters, it recalls the previous pattern. If you press it a third time or you press it when an incremental search has already found a match, VS.NET searches for the next occurrence.
Alt-F3, C
Selects or clears the Match Case option for Find and Replace operations
Alt-F3, R
Selects or clears the Regular Expression option so that special characters can be used in Find and Replace operations
Ctrl-H
Displays the Replace dialog
Ctrl-Shift-H
Displays the Replace in Files dialog
Ctrl-Shift-I
Performs an incremental search in reverse direction
Alt-F3, S
Halts the current Find in Files operation
Alt-F3, B
Selects or clears the Search Up option for Find and Replace operations
Alt-F3, W
Selects or clears the Match Whole Word option for Find and Replace operations
Alt-F3, P
Selects or clears the Wildcard option for Find and Replace operations
Help
Shortcut Description
Ctrl-Alt-F1
Displays the Contents window for the documentation
Ctrl-F1
Displays the Dynamic Help window, which displays different topics depending on what items currently have focus. If the focus is in a source window, the Dynamic Help window will display help topics that are relevant to the text under the cursor
F1
Displays a topic from Help that corresponds to the part of the user interface that currently has the focus. If the focus is in a source window, Help will try to display a topic relevant to the text under the cursor
Ctrl-Alt-F2
Displays the Help Index window
Shift-Alt-F2
Displays the Index Results window, which lists the topics that contain the keyword selected in the Index window
Alt-Down Arrow
Displays the next topic in the table of contents. Available only in the Help browser window
Alt-Up Arrow
Displays the previous topic in the table of contents. Available only in the Help browser window
Ctrl-Alt-F3
Displays the Search window, which allows you to search for words or phrases in the documentation
Shift-Alt-F3
Displays the Search Results window, which displays a list of topics that contain the string searched for from the Search window.
Shift-F1
Displays a topic from Help that corresponds to the user interface item that has the focus
Debugging
Shortcut Description
Ctrl-Alt-V, A
Displays the Auto window to view the values of variables currently in the scope of the current line of execution within the current procedure
Ctrl-Alt-Break
Temporarily stops execution of all processes in a debugging session. Available only in run mode
Ctrl-Alt-B
Displays the Breakpoints dialog, where you can add and modify breakpoints
Ctrl-Alt-C
Displays the Call Stack window to display a list of all active procedures or stack frames for the current thread of execution. Available only in break mode
Ctrl-Shift-F9
Clears all of the breakpoints in the project
Ctrl-Alt-D
Displays the Disassembly window
Ctrl-F9
Enables or disables the breakpoint on the current line of code. The line must already have a breakpoint for this to work
Ctrl-Alt-E
Displays the Exceptions dialog
Ctrl-Alt-I
Displays the Immediate window, where you can evaluate expressions and execute individual commands
Ctrl-Alt-V, L
Displays the Locals window to view the variables and their values for the currently selected procedure in the stack frame
Ctrl-Alt-M, 1
Displays the Memory 1 window to view memory in the process being debugged. This is particularly useful when you do not have debugging symbols available for the code you are looking at. It is also helpful for looking at large buffers, strings, and other data that does not display clearly in the Watch or Variables window
Ctrl-Alt-M, 2
Displays the Memory 2 window
Ctrl-Alt-M, 3
Displays the Memory 3 window
Ctrl-Alt-M, 4
Displays the Memory 4 window
Ctrl-Alt-U
Displays the Modules window, which allows you to view the .dll or .exe files loaded by the program. In multiprocess debugging, you can right-click and select Show Modules for all programs
Ctrl-B
Opens the New Breakpoint dialog
Ctrl-Alt-Q
Displays the Quick Watch dialog with the current value of the selected expression. Available only in break mode. Use this command to check the current value of a variable, property, or other expression for which you have not defined a watch expression
Ctrl-Alt-G
Displays the Registers window, which displays CPU register contents
Ctrl-Shift-F5
Terminates the current debugging session, rebuilds if necessary, and then starts a new debugging session. Available in break and run modes
Ctrl-Alt-N
Displays the Running Documents window that displays the set of HTML documents that you are in the process of debugging. Available in break and run modes
Ctrl-F10
Starts or resumes execution of your code and then halts execution when it reaches the selected statement. This starts the debugger if it is not already running
Ctrl-Shift-F10
Sets the execution point to the line of code you choose
Alt-NUM *
Highlights the next statement to be executed
F5
If not currently debugging, this runs the startup project or projects and attaches the debugger. If in break mode, this allows execution to continue (i.e., it returns to run mode).
Ctrl-F5
Runs the code without invoking the debugger. For console applications, this also arranges for the console window to stay open with a "Press any key to continue" prompt when the program finishes
F11
Executes code one statement at a time, tracing execution into function calls
Shift-F11
Executes the remaining lines of a function in which the current execution point lies
F10
Executes the next line of code but does not step into any function calls
Shift-F5
Available in break and run modes, this terminates the debugging session
Ctrl-Alt-V, T
Displays the This window, which allows you to view the data members of the object associated with the current method
Ctrl-Alt-H
Displays the Threads window to view all of the threads for the current process
F9
Sets or removes a breakpoint at the current line
Ctrl-F11
Displays the disassembly information for the current source file. Available only in break mode
Ctrl-Alt-W, 1
Displays the Watch 1 window to view the values of variables or watch expressions
Ctrl-Alt-W, 2
Displays the Watch 2 window
Ctrl-Alt-W, 3
Displays the Watch 3 window
Ctrl-Alt-W, 4
Displays the Watch 4 window
Ctrl-Alt-P
Displays the Processes dialog, which allows you to attach or detach the debugger to one or more running processes
Object browser
Shortcut Description
Alt-F12
Displays the Find Symbol dialog
Ctrl-F12
Displays the declaration of the selected symbol in the code
F12
Displays the definition for the selected symbol in code
Ctrl-Alt-F12
Displays the Find Symbol Results window
Ctrl-Alt-J
Displays the Object Browser to view the classes, properties, methods, events, and constants defined either in your project or by components and type libraries referenced by your project
Alt-+
Moves back to the previously selected object in the selection history of the object browser
Shift-Alt-+
Moves forward to the next object in the selection history of the object browser
Tool window
Shortcut Description
Ctrl-Shift-M
Toggles the Command window into or out of a mode allowing text within the window to be selected
Ctrl-Shift-C
Displays the Class View window
Ctrl-Alt-A
Displays the Command window, which allows you to type commands that manipulate the IDE
Ctrl-Alt-T
Displays the Document Outline window to view the flat or hierarchical outline of the current document
Ctrl-Alt-F
Displays the Favorites window, which lists shortcuts to web pages
Ctrl-Alt-O
Displays the Output window to view status messages at runtime
F4
Displays the Properties window, which lists the design-time properties and events for the currently selected item
Shift-F4
Displays the property pages for the item currently selected. (For example, use this to show a project's settings.)
Ctrl-Shift-E
Displays the Resource View window
Ctrl-Alt-S
Displays the Server Explorer window, which allows you to view and manipulate database servers, event logs, message queues, web services, and many other operating system services
Ctrl-Alt-R
Displays the web browser window, which allows you to view pages on the Internet
Ctrl-Alt-L
Displays the Solution Explorer, which lists the projects and files in the current solution
Ctrl-Alt-K
Displays the TaskList window, which displays tasks, comments, shortcuts, warnings, and error messages
Ctrl-Alt-X
Displays the Toolbox, which contains controls and other items that can be dragged into editor and designer windows
Html editor (Design View)
Shortcut Description
Ctrl-B
Toggles the selected text between bold and normal
Ctrl-Shift-T
Decreases the selected paragraph by one indent unit
Ctrl-T
Indents the selected paragraph by one indent unit
Ctrl-I
Toggles the selected text between italic and normal
Ctrl-Shift-K
Prevents an absolutely positioned element from being inadvertently moved. If the element is already locked, this unlocks it
Ctrl-G
Toggles the grid
Ctrl-Shift-G
Specifies that elements be aligned using an invisible grid. You can set grid spacing on the Design pane of HTML designer options in the Options dialog, and the grid will be changed the next time you open a document
Ctrl-U
Toggles the selected text between underlined and normal
Ctrl-Shift-L
Displays the Bookmark dialog
Ctrl-J
Inserts <div></div> in the current HTML document
Ctrl-L
When text is selected, displays the Hyperlink dialog
Ctrl-Shift-W
Displays the Insert Image dialog
Ctrl-Alt-Up Arrow
Adds one row above the current row in the table
Ctrl-Alt-Down Arrow
Adds one row below the current row in the table
Ctrl-Alt-Left Arrow
Adds one column to the left of the current column in the table
Ctrl-Alt-Right Arrow
Adds one column to the right of the current column in the table
Ctrl-Shift-Q
Toggles display of marker icons for HTML elements that do not have a visual representation, such as comments, scripts, and anchors for absolutely positioned elements
Ctrl-Page Down
Switches from design view to HTML view and vice versa
Ctrl-Q
Displays a 1-pixel border around HTML elements that support a BORDER attribute and have it set to zero, such as tables, table cells, and divisions
Macro
Shortcut Description
Alt-F8
Displays the Macro Explorer window, which lists all available macros
Alt-F11
Launches the macros IDE
Ctrl-Shift-R
Places the environment in macro record mode or completes recording if already in record mode
Ctrl-Shift-P
Plays back a recorded macro
Summary: Visual Studio 2005 is the leading developer tool on the market, and I would like to share with you some tips and tricks that will make this great tool even more powerful. Getting familiar with a tool is crucial to getting the most out of the tool, and development tools and IDEs are no different. But with the many new technologies such as C# 2.0, ASP .NET 2.0, Windows Workflow Foundation, Windows Presentation Foundation, and Windows Communication Foundation, who has time to learn about Visual Studio itself? By spending 10 minutes to read this article, I hope you will learn a couple of useful things that will make your life inside Visual Studio more pleasant and productive. (19 printed pages)

Contents

Keeping Your Hands on the Keyboard
Window Layout Selector
Code Snippets
Customizing Visual Studio Start Page
Team Settings
/resetuserdata Switch
Conclusion

Keeping Your Hands on the Keyboard

favorite keyboard shortcuts

Ever wished that you never have to take your hands off the keyboard when you are doing your development inside Visual Studio? If you are a power user, you will certainly enjoy the using keyboard shortcuts to perform various operations more quickly. I am sure most of you are already familiar with some of them: F5 for Debug.Start, F10 for Debug.StepOver, F4 for View.Properties. There are several other very useful keyboard shortcuts that are less known. I have included some of my favorite ones in the table below.

Keyboard Shortcut Command
F7 Toggles between design and code views.
F9 Toggles breakpoint.
F12 Go to definition of a variable, object, or function.
Ctrl+Shift+7

Ctrl+Shift+8

Quickly navigate forward and backwards in the go to definition stack.
Shift+F12 Find all references of a function or a variable.
Ctrl+M, Ctrl+M Expand and collapse code outlining in the editor.
Ctrl+K, Ctrl+C

Ctrl+K, Ctrl+U

Comment and uncomment line(s) of code, respectively.
Shift+Alt+Enter Toggles between full screen mode and normal mode.
Ctrl+I Incremental Search.

Creating a keyboard shortcuts cheat sheet

Most people don't know this, but there are actually over 450 keyboard shortcuts in Visual Studio by default. But there is no easy way to find out all the keyboard shortcuts inside Visual Studio. You can find out what all the default keyboard shortcuts are by writing a simple macro to enumerate all of them. The following (Listing 1) shows the code for this.

Public Module Module1

    Public Sub ListShortcutsInHTML()

        'Declare a StreamWriter
       
            Dim sw As System.IO.StreamWriter
        sw = New StreamWriter("c:\\demo\\Shortcuts.html")

        'Write the beginning HTML
        WriteHTMLStart(sw)

        ' Add a row for each keyboard shortcut
        For Each c As Command In DTE.Commands
            If c.Name <> "" Then
                            Dim bindings As System.Array                 bindings = 
                    CType(c.Bindings, System.Array)
                For i As Integer = 0 To bindings.Length - 1
                    sw.WriteLine("<tr>")
                    sw.WriteLine("<td>" + c.Name + "</td>")
                    sw.WriteLine("<td>" + bindings(i) + "</td>")
                    sw.WriteLine("</tr>")
                Next End If Next 
        'Write the end HTML
        WriteHTMLEnd(sw)

        'Flush and close the stream
        sw.Flush()
        sw.Close()
   End Sub
Public Sub WriteHTMLStart(ByVal sw As System.IO.StreamWriter)
        sw.WriteLine("<html>")
        sw.WriteLine("<head>")
        sw.WriteLine("<title>")

        sw.WriteLine("Visual Studio Keyboard Shortcuts")
        sw.WriteLine("</title>")
        sw.WriteLine("</head>")

        sw.WriteLine("<body>")
        sw.WriteLine("<h1>Visual Studio 2005 Keyboard Shortcuts</h1>")
        sw.WriteLine("<font size=""2"" face=""Verdana"">")
        sw.WriteLine("<table border=""1"">")
        sw.WriteLine("<tr BGCOLOR=""#018FFF""><td 
align=""center""><b>Command</b></td><td 
align=""center""><b>Shortcut</b></td></tr>")


    End Sub 
    Public Sub WriteHTMLEnd(ByVal sw As System.IO.StreamWriter)
        sw.WriteLine("</table>")
        sw.WriteLine("</font>")
        sw.WriteLine("</body>")
        sw.WriteLine("</html>")
    End Sub 
End Module 

Listing 1. Macro to generate Keyboard Shortcuts in HTML

To use this macro, go to Tools, select Macros, and then choose Macros IDE. . . to launch the Macros IDE. Expand the MyMacros project, MyMacros namespace and double-click on Module1. Simply copy Listing 1 to the Macros IDE and run the macro. After running the macro, you would have produced a keyboard shortcuts reference for Visual Studio. Open your output at C:\demo\Shortcuts.html. Figure 1 shows the partial output. Feel free to print it out and post it near your computer as you learn new keyboard shortcuts.

Figure 1. Partial listing of Visual Studio 2005 Keyboard Shortcuts

Customizing Keyboard Shortcuts

If you have a favorite keyboard shortcut that is not mapped by default, you can always customize it through by clicking Tools > Options... > Environment > Keyboard (see Figure 2). However, if you add a lot of keyboard shortcuts to your environment, you can do this more easily by editing your auto-save settings file directly. You can do this by performing the following:

Figure 2. Options dialog - customize Keyboard Shortcuts

Step 1: Export current Keyboard Shortcuts. Go to Tools > Import and Export Settings. . . to start the Import/Export Settings Wizard. Choose "Export selected environment settings" and click Next. Click on "All Settings" to deselect all the checkboxes, and then expand the Options, Environment nodes to select the "Keyboard" checkbox (Figure 3). Click Next to go to the last page of the Wizard. Name the new settings file "MyKeyboardShorcuts.vssettings" and leave the path as the default directory (Figure 4). Click Finish.

Figure 3. Select only the Keyboard settings category to export

Figure 4. Renaming the settings file to MyKeyboardShortcuts.vssettings

Step 2: Open and edit the settings file. The file is located at My Documents\Visual Studio 2005\Settings\MyKeyboardShortcuts.vssettings. The Visual Studio settings files are just XML files and you can open this with any text editor. I recommend that you open the file with Visual Studio itself, as this will give you syntax coloring and document formatting capabilities. Once you have the file opened, hit "Ctrl+K, Ctrl+D" to have Visual Studio automatically format it. Then, look for the <UserShortcuts> tag. Within this XML element, you can add your own list of shortcuts. An example is shown in Listing 2 below.

...
<UserShortcuts> <Shortcut Command="View.CommandWindow"
    Scope="Global"> Ctrl+W, Ctrl+C </Shortcut>
    <Shortcut Command="View.SolutionExplorer"
    Scope="Global">
Ctrl+W, Ctrl+S
</Shortcut>
   <Shortcut Command="View.ErrorList" Scope="Global">
Ctrl+W, Ctrl+E
</Shortcut>
   <Shortcut Command="View.TaskList" Scope="Global">
Ctrl+W, Ctrl+T
</Shortcut>
   <Shortcut Command="View.Output" Scope="Global">
Ctrl+W, Ctrl+O
</Shortcut>
</UserShortcuts>
...

Listing 2. Adding Keyboard Shortcuts directly in the settings file

The XML here is quite easy to understand. You simply have a <Shortcut> element for each of the shortcuts that you want to add. You specify the shortcut itself as this element's content, and you can use modifier keys such as Shift, Ctrl, Alt together by chaining them with the "+" character (for example, Ctrl+Alt+J). You specify the canonical command name of the command that you want to bind the shortcut to in the Command attribute. The Scope attribute will almost always be Global, so we will not discuss that anymore. The most difficult part of this exercise is probably figuring out what the canonical name is for a particular command. The canonical name of a particular command is formed by concatenating the top level menu name with the "." character, and the command name in camel case without any spaces.

After you have added all your shortcuts, save the file.

Step 3: Import the settings file. Now that you have added your shortcuts in your settings file, you can import it back to your environment. Of course, you can also share your settings file with others. Start the Import and Export settings Wizard again, but choose "Import selected environment settings' this time; click Next. Select "No, just import new settings, overwriting my current settings" and click Next. Choose "MyKeyboardShortcuts.vssettings" under the "My Settings" folder and click Next. Have the default selections remain and click Finish.

Showing Shortcuts on ToolTips

You can actually tell the environment to show shortcuts on the ToolTips that are available when you mouse over commands on toolbars. Go to Tools > Customize. . ., and make sure the option Show shortcut keys in ScreenTips is checked.

Figure 5. Turning on Show shortcut keys in ToolTips

Window Layout Selector

Visual Studio is a powerful environment with many different tool windows for different tasks and purposes. This is especially true with the new Team System features shipping in VS 2005. We have heard from many customers that it would be useful if there was a way to quickly switch between different window layouts to match their current task at hand. You can actually create this functionality yourself inside VS 2005, but it will involve several steps.

Step 1. Create settings files. Visual Studio 2005 has a new feature that allows you to import/export environment settings. Virtually all customizations that you can make to the environment can be exported to a file so that you can share them with others, import them on a different computer or store it as a backup. The settings that you can import/export include window layout, keyboard shortcuts, menu customizations, fonts & colors and virtually everything in the Options dialog (Tools > Options. . . ). You either export all the environment settings or only a subset of these settings anytime you wish.

In creating our Window Selector, the first step is to create a separate settings file for each of the window layout that you wish to use. In this example, I will create 3 settings file corresponding to the 3 window layouts that I wish to use: CodeWriting, CodeBrowsing, and FormsDesign.

First, simply arrange the window layout the way you prefer when you write code. For me, I prefer to set all the visible tool windows to the auto hide state to maximize coding space. Figure 6 shows how I have arranged the tool windows for this window layout, but feel free to adapt this to your preference. Then, go to Tools > Import and Export Settings to start the Import and Export Settings Wizard. Choose Export selected environment settings and click Next. Select only the window layout checkbox and then click Next. Name the setting CodeWritingWinLayout.vssettings and click Finish. Now you have created the first of three settings files that you need. Repeat the above steps to create the remaining two settings files. Obviously, you need to change the window layout and name the files differently. I have named mine CodeBrowsingWinLayout.vssettings and FormsDesignWinLayout.vssettings.

 

Click here for larger image

Figure 6. Window layout for coding (click image to enlarge)

Step 2. Create macros to import settings files. Once the settings files have been created, you need to create 3 macros – 1 to import each of the settings file. Listing 3 below shows how trivial this code is.

Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports System.IO

Public Module Module1

  Public Sub ImportWinLayoutCodeWriting()
    DTE.ExecuteCommand("Tools.ImportandExportSettings",
        "-import:c:\demo\settings\CodeWritingWinLayout.vssettings")
  End Sub

  Public Sub ImportWinLayoutCodeBrowsing()
    DTE.ExecuteCommand("Tools.ImportandExportSettings",
        "-import:c:\demo\settings\CodeBrowsingWinLayout.vssettings")
  End Sub 
  Public Sub ImportWinLayoutFormsDesign()
    DTE.ExecuteCommand("Tools.ImportandExportSettings",
        "-import:c:\demo\settings\FormsDesignWinLayout.vssettings")
End Sub 
End Module 

Listing 3. Macro code for importing a settings file

Step 3. Add buttons to the toolbar. Now it's time to create the actual buttons that will change your window layout. Click on Tools > Customize. . ., click on the Commands tab. Select Macros from the Categories list box, and then scroll down on the list of Commands until you find the three macros that you just wrote. They should be called MyMacros.Module1.ImportWinLayoutCodeWriting, MyMacros.Module1.ImportWinLayoutCodeBrowsing, and MyMacros.Module1.ImportWinLayoutFormsDesign (see Figure 7). Click and drag each of these commands onto the Visual Studio toolbar. You may now want to right-click on the newly placed commands on the toolbar and change the names of these commands to something shorter.

Figure 7. Use Customize dialog to place macros onto the toolbar

Close the Customize dialog to save your customizations. You are done with creating your own Window Layout selector. Click on the new buttons on the toolbar and try it out. You can even assign a keyboard shortcut to these commands by going to Tools > Options. . . > Environment > Keyboard page.

Code Snippets

Code snippets are one of the best productivity features introduced in Visual Studio 2005. It allows you to quickly insert fragments of code to avoid tedious typing (such as typing a for loop) or to give you a template of how to accomplish a certain task (such as sending data over the network). Most of the built-in C# snippets are of the first type – they help you in minimizing repetitive typing, while most of the built-in VB snippets are of the second type – they let you code up a specific task more easily.

There are two ways to insert a snippet. You can type the snippet's alias in the code editor and press Tab twice (you only need to press Tab once for VB) to insert the snippet immediately. After the code snippet has been inserted, you can press Tab and Shift+Tab to jump to different fields within the snippet. This allows you to quickly change the parts of code that need to be modified. Notice that in C#, code snippet aliases also have IntelliSense. You can tell that an item is a code snippet in the IntelliSense list by its snippet icon.

Figure 8. IntelliSense fully supports code snippets

If you don't remember your code snippet's alias, you can also insert it by pressing "Ctrl+K, Ctrl+X" within the code editor or do a mouse right-click with the mouse and select Insert Snippet.... This shows the code snippet picker, which enables you to browse all the snippets that are applicable to your current programming language and to choose the one you want to insert. This method of inserting code snippets works for both C# and Visual Basic. Visual Basic users have yet another way to insert snippets: you can type the first few letters of a snippet alias, followed by "?" and pressing Tab. Visual Studio will display an alphabetical listing of all the code snippet aliases with the one most closely matched highlighted. This feature is available only for Visual Basic users.

 

Click here for larger image

Figure 9. Inserting a code snippet in C# (click image to enlarge)

Personally, the most exciting part of the code snippet feature is that you can create your own snippets for your personal use or share them with the community. Of course, you can also download code snippets that other developers have created.

It is very easy to create your own code snippet right inside Visual Studio. I will show you how you can do this via an example. I frequently write quick-and-dirty utilities to help me do my work. Many of these utilities have a common patter: open a file, do some processing, and then close the file. Here is how I would create my snippet.

Step 1: Create the XML file. Each code snippet is contained within an XML file. Inside Visual Studio, simply go to File > New. . . > File. . ., and then choose the XML File type.

Figure 10. Creating a new XML file

Step 2: Define the snippet. Interestingly enough, there is even a snippet to create a snippet. Simply press Ctrl+K, Ctrl+X on the second line of the file and choose the Snippet code snippet, the template of a code snippet file is automatically inserted for you.

 

Click here for larger image

Figure 11. Using XML snippet to create other snippets (click image to enlarge)

The title, author, shortcut, and description fields are pretty self-explanatory and I will not go into detail with those. The contents within the <Snippet> tags deserve some discussion and can be best explained by my example below.

Essentially, you put all your code inside the <![CDATA[...]]> tag, which is inside the </Code> tag. For fields that you want the user to be able to easily replace, you put a pair of "$" characters around them. In my example, I have three literals that I want users of my snippet to easily replace: StrmReader, FilePath, and Line. These three literals are used within the CDATA section with a pair of "$" characters surrounding them. In addition, each of these literals must be defined within the <Declarations> element. Each is given an ID and an optional default value.

The astute reader will notice that there is also another literal inside my code snippet that I did not define: $end$. This is a special literal that specifies where the cursor will be located when the user presses Enter after they have completed filling in the snippet fields. There is also another special literal that I am not showing here: $selected$. The $selected$ literal is meaningful only for code snippets that are of the SurroundsWith type. It defines where the selected code segment will be placed when this snippet is inserted by using Surround With...

<?xml version="1.0"
    encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>File
     Processing</Title> <Author>James Lau</Author>
     <Shortcut>fp</Shortcut> <Description>Opens a file, does some processing, and then closes the file.</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
     <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>StrmReader</ID> <Default>strmReader</Default> </Literal> <Literal>
     <ID>FilePath</ID> <Default>fPath</Default> </Literal> <Literal>
     <ID>Line</ID> <Default>strLine</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp">       <![CDATA[         
   StreamReader $StrmReader$ = null;
   try    {
      $StrmReader$ = new StreamReader($FilePath$);
      string $Line$;
      while (($Line$ = $StrmReader$.ReadLine()) != null)
      {
         // Perform some processing
         $selected$
         $end$
      }
   }
   catch (IOException ioex)
   {
      // Handle exception
   }
   finally
   {
      $StrmReader$.Close();
   }
    ]]>
    </Code>
  </Snippet>
</CodeSnippet>

Listing 4. Sample code snippet content

Customizing Visual Studio Start Page

The new Start Page in Visual Studio 2005 not only contains a live RSS feed that provides up-to-date information on MSDN news. If you prefer to read some other RSS feed on the Start Page, you can customize the RSS news channel by selecting Tools, then selecting Options. . ., selecting Environment, and then choosing Startup page, where you can edit the URL under Start Page news channel. If you prefer to not have the Start Page automatically be displayed each time you launch Visual Studio, you can also change this behavior by choosing Show empty environment under At startup on the same options page.

Team Settings

Another new but less known feature in Visual Studio 2005 is Team Settings. If you work in a team environment (and most of us do), then Team Settings may be able to help you in enforcing team coding rules or in setting up Visual Studio more quickly.

Let's assume that you would like to enforce a basic set of code formatting rules within your team. Instead of specifying what these rules are and have each team member customize the IDE options to comply with those rules, you can simply create a settings file and have your team members point to it. Whenever the team settings file is updated, it will automatically be imported over the user's existing settings the next time he or she starts Visual Studio. Here is what you do to leverage the power of this feature.

Step 1: Create settings file. You can use Team Settings to enforce any IDE customizations you like. The most common settings we expect developers to use Team Settings for are the code formatting settings. But you can use this feature for any Visual Studio settings that can be exported, such as Fonts & Colors, SourceSafe settings, keyboard shortcuts, menu customizations, etc. Simply customize the desired settings within Visual Studio, and then use Tools > Import/Export Settings. . . to export them to a known location. It is important that you only export the set of settings that you would like to share with your team.

Step 2: Place settings file in UNC path. Copy the settings file you exported from Step 1 to a network path that your team members have access to. On my machine, I have shared my team settings file at \\jameslau\public\teamsettings.settings.

Step 3: Change Team Settings path. Have your team members change their Team Settings path to point to your team settings file. They can do this by going to Tools > Options. . . > Environment > Import and Export Settings. Select (check) the Use team settings file check box and specify the path of the team settings file.

Figure 12. Options dialog for changing Team Settings path

/resetuserdata Switch

The last tip that I will share with you concerns the /resetuserdata switch. You can use this switch to reset Visual Studio to its out-of-box state if Visual Studio ever runs into a damaged state that you cannot recover from. Examples of these problems may be a corrupted window layout file, corrupted menu customization file, or corrupted keyboard shortcuts file. Disclaimer: you will lose all your environment settings and customizations if you use this switch. It is for this reason that this switch is not officially supported and Microsoft does not advertise this switch to the public (you won't see this switch if you type devenv.exe /? in the command prompt). You should only use this switch as the last resort if you are experiencing an environment problem, and make sure you back up your environment settings by exporting them before using this switch.

To use this switch, do the following:

  1. Shut down all instances of Visual Studio 2005.
  2. Click Start, the choose Run....
  3. Type "devenv.exe /resetuserdata".

This command will take a couple of minutes to run as Visual Studio cleans up and sets itself back to its original state. You may open Task Manager at this point to check whether the devenv.exe process is still running. After it has completed running, you can restart Visual Studio. You will then be greeted by the first launch dialog again, as if you are running Visual Studio for the first time on your machine.

Colclusion:it is just simple to work with visual studio but working smartly is possible only by using above defined shortcut keys,hope all is well and useful.