Upload
krishnamraju-nadakuditi
View
223
Download
0
Embed Size (px)
Citation preview
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
1/19
Visual Studio 2005 IDE Tips and Tricks
James Lau
Program Manager, Microsoft
December 2006
Applies to:Microsoft Visual Studio 2005
Summary: Visual Studio 2005 is the leading developer tool on the market, and Iwould 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 newtechnologies 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 hopeyou 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 SelectorCode Snippets
Customizing Visual Studio Start PageTeam Settings
/resetuserdata Switch
Conclusion
Keeping Your Hands on the Keyboard
My 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 willcertainly 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 forDebug.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.
http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic1%23vs05idetips_topic1http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic2%23vs05idetips_topic2http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic3%23vs05idetips_topic3http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic4%23vs05idetips_topic4http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic5%23vs05idetips_topic5http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic6%23vs05idetips_topic6http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic7%23vs05idetips_topic7http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic1%23vs05idetips_topic1http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic2%23vs05idetips_topic2http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic3%23vs05idetips_topic3http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic4%23vs05idetips_topic4http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic5%23vs05idetips_topic5http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic6%23vs05idetips_topic6http://msdn.microsoft.com/en-us/library/bb245788(VS.80).aspx#vs05idetips_topic7%23vs05idetips_topic78/9/2019 Visual Studio 2005 IDE Tips and Tricks
2/19
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 stac
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 inVisual 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.StreamWritersw = 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("")
sw.WriteLine("" + c.Name + "")
sw.WriteLine("" + bindings(i) + "")
sw.WriteLine("")
Next
End If
Next
'Write the end HTML
WriteHTMLEnd(sw)
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
3/19
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
4/19
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 filedirectly. You can do this by performing the following:
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
5/19
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 selectedenvironment 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 asthe default directory (Figure 4). Click Finish.
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
6/19
Figure 3. Select only the Keyboard settings category to export
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
7/19
Figure 4. Renaming the settings file to MyKeyboardShortcuts.vssettings
Step 2: Open and edit the settings file. The file is located at MyDocuments\Visual Studio 2005\Settings\MyKeyboardShortcuts.vssettings. The Visual
Studio settings files are just XML files and you can open this with any text editor. Irecommend 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 tag. Within this XML element, you can add your own list of
shortcuts. An example is shown in Listing 2 below.
...
Ctrl+W, Ctrl+C
Ctrl+W, Ctrl+S
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
8/19
Ctrl+W, Ctrl+E
Ctrl+W, Ctrl+T
Ctrl+W, Ctrl+O
...
Listing 2. Adding Keyboard Shortcuts directly in the settings file
The XML here is quite easy to understand. You simply have a 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 togetherby 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 wewill not discuss that anymore. The most difficult part of this exercise is probablyfiguring 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 namewith 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 alsoshare 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 clickNext.Choose "MyKeyboardShortcuts.vssettings" under the "My Settings" folder andclick 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.
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
9/19
Figure 5. Turning on Show shortcut keys in ToolTips
Window Layout Selector
Visual Studio is a powerful environment with many different tool windows fordifferent 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 beuseful 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 yourselfinside VS 2005, but it will involve several steps.
Step 1. Create settings files. Visual Studio 2005 has a new feature that allows youto 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 settingsthat 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 ofthese settings anytime you wish.
In creating our Window Selector, the first step is to create a separate settings file foreach 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 touse: CodeWriting, CodeBrowsing, and FormsDesign.
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
10/19
First, simply arrange the window layout the way you prefer when you write code. Forme, 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 windowlayout, 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. ChooseExportselected environment settings and click Next. Select only the window layout
checkbox and then click Next. Name thesetting CodeWritingWinLayout.vssettings and click Finish. Now you havecreated 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 namedmine CodeBrowsingWinLayout.vssettings and FormsDesignWinLayout.vssetti
ngs.
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 3below 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
http://msdn.microsoft.com/en-us/library/Bb245788.vs05idetips6(l=en-US,v=VS.80).gif8/9/2019 Visual Studio 2005 IDE Tips and Tricks
11/19
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 thatwill change your window layout. Click on Tools > Customize. . ., click on
the Commands tab. Select Macros from the Categories list box, and then scrolldown 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,andMyMacros.Module1.ImportWinLayoutFormsDesign (se
e 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 andchange the names of these commands to something shorter.
Figure 7. Use Customize dialog to place macros onto the toolbar
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
12/19
Close the Customize dialog to save your customizations. You are done with creatingyour 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 SnippetsCode 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 (suchas 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 thefirst 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 moreeasily.
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 thesnippet 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 toquickly 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 languageand 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 toinsert 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
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
13/19
snippet aliases with the one most closely matched highlighted. This feature isavailable only for Visual Basic users.
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 createyour 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 tohelp 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.
http://msdn.microsoft.com/en-us/library/Bb245788.vs05idetips9(l=en-US,v=VS.80).gif8/9/2019 Visual Studio 2005 IDE Tips and Tricks
14/19
Figure 10. Creating a new XML file
Step 2: Define the snippet. Interestingly enough, there is even a snippet to createa 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 automaticallyinserted for you.
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
15/19
Figure 11. Using XML snippet to create other snippets (click image toenlarge)
The title, author, shortcut, and description fields are pretty self-explanatory and I
will not go into detail with those. The contents within the tags deserve
some discussion and can be best explained by my example below.
Essentially, you put all your code inside the tag, which is inside
the 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 withinthe 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 snippetthat 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 thesnippet 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...
http://msdn.microsoft.com/en-us/library/Bb245788.vs05idetips11(l=en-US,v=VS.80).gif8/9/2019 Visual Studio 2005 IDE Tips and Tricks
16/19
File Processing
James Lau
fp
Opens a file, does some processing, and then closes
the file.
SurroundsWith
Expansion
StrmReader
strmReader
FilePath
fPath
Line
strLine
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();
}]]>
Listing 4. Sample code snippet content
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
17/19
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 otherRSS feed on the Start Page, you can customize the RSS news channel by
selecting Tools, then selecting Options. . ., selectingEnvironment, and thenchoosing 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 eachtime 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 ruleswithin your team. Instead of specifying what these rules are and have each teammember 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 settingsfile 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 powerof this feature.
Step 1: Create settings file. You can use Team Settings to enforce any IDEcustomizations 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 forany Visual Studio settings that can be exported, such as Fonts & Colors, SourceSafe
settings, keyboard shortcuts, menu customizations, etc. Simply customize thedesired settings within Visual Studio, and then use Tools > Import/ExportSettings. . . 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 fromStep 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 goingto Tools > Options. . . > Environment > Import and Export Settings. Select
(check) the Use team settings file check box and specify the path of the teamsettings file.
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
18/19
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 problemsmay be a corrupted window layout file, corrupted menu customization file, or
corrupted keyboard shortcuts file. Disclaimer: you will lose all your environmentsettings 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 thepublic (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 anenvironment 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 tocheck whether the devenv.exe process is still running. After it has completed
8/9/2019 Visual Studio 2005 IDE Tips and Tricks
19/19
running, you can restart Visual Studio. You will then be greeted by the first launchdialog again, as if you are running Visual Studio for the first time on your machine.
Conclusion
We are working hard to continue to bring you useful productivity features in VisualStudio. I hope these tips are useful to you and can help you in becoming a VisualStudio power user. If you have comments, feedback, or suggestions for Visual Studio
IDE issues, I would love to hear from you. You can write [email protected].
mailto:[email protected]:[email protected]